Scikit_Learn introduction and walkthrough

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xiewenrui1996/article/details/101034742

A, scikit-learn Profile

scikit-learn is based on a built Scipy Python module for machine learning. In various application areas, we have extended a large number of tools based on Scipy package, they collectively known as Scikits. In all branches versions, scikit-learn is the most famous, is open source, anyone can use the library free of charge or for secondary development. sklearn there are many models already do a good job, you can look directly Quguan network model also download directly down.

scikit-learn includes many top machine learning algorithms, there are six basic functions, namely, classification, regression, clustering, data reduction, model selection and data preprocessing. scikit-learn has a very active user community, basically all of its functions are very detailed documentation for users to review. Can read scikit-learn user guides and documentation, its use algorithms more fully understood.

Second, the library introduced scikit-Learn

%matplotlib inline
import numpy as np
import scipy
import sklearn  #导入scikit—learn库

scikit-learn many data sets can be downloaded, following selection of a code to use

%matplotlib inline
import numpy as np
import scipy
import sklearn


from sklearn.datasets import load_iris  #花的分类
iris=load_iris()
n_samples,n_datas=iris.data.shape
print(n_samples,n_datas)
print(iris.target_names)

##输出 150 4
['setosa' 'versicolor' 'virginica']

import matplotlib.pyplot as plt
x_index=1   #第二列
y_index=2
plt.scatter(iris.data[:,x_index],iris.data[:,y_index],c=iris.target)
plt.xlabel(iris.feature_names[x_index])
plt.ylabel(iris.feature_names[y_index])

The output image is:

from sklearn.datasets import load_digits
digits=load_digits()
#这个是手写数字的训练集,标签为1~9

Third, the model instances, model selection

 

Model general import library:

from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor

General procedure is to first create an object, and then fit (case), and then predict predict (), and finally scatter plot scatter in the picture () as described in the program

import numpy as np
import matplotlib.pyplot as plt
import sklearn
from sklearn.linear_model import LinearRegression
clf=LinearRegression()
from sklearn.datasets import load_boston
data=load_boston()

n_samples,n_features=data.data.shape
print(n_samples,n_features)
print(data.target.shape)
column_i=5
plt.scatter(data.data[:,column_i],data.target)
print(data.feature_names[5])
from sklearn.metrics import mean_absolute_error
clf.fit(data.data,data.target)
predicted=clf.predict(data.data)
mean_absolute_error(data.target,predicted)
plt.scatter(data.target,predicted)
plt.xlabel("ture prices")
plt.ylabel('predicted prices')
plt.plot(data.target,predicted,color='red')

Output:

Model selection

Guess you like

Origin blog.csdn.net/xiewenrui1996/article/details/101034742