Scikit-learn library for beginners in python

The scikit-learn library is a library related to machine learning. It is a powerful machine learning toolkit under python. It provides a complete machine learning toolbox, including data preprocessing, classification, regression, clustering, prediction, model analysis, etc. .

note:

scikit-learn relies on numpy, scipy and matplotlib, so you need to install these libraries in advance, and then install scikit-learn.

One, install the scikit-learn library

As mentioned earlier, scikit-learn relies on numpy, scipy and matplotlib, so you need to install these libraries in advance, and then install scikit-learn.

The simplest pip installation method is used here. For other methods, please refer to my blog "Methods of downloading and installing third-party libraries for python".
Insert picture description here
Since I have installed it before, the prompt "installed" is displayed.

Second, use the scikit-learn library

After installing the scikit-learn library as described above, you can use this powerful library. I will write some examples of my own practice here:

1. Use scikit-learn to create a machine learning model

Code:

from sklearn.linear_model import LinearRegression #导入线性回归模型
model = LinearRegression() #建立先行回归模型
print(model)

Output result:
Insert picture description here
2. Import the iris data set and use the data to train the SVM model

Code snippet 1:

from sklearn import datasets #导入数据集

iris = datasets.load_iris() #加载数据集
print(iris.data.shape) #查看数据集大小

Output result:
Insert picture description here
code segment 2:

from sklearn import svm #导入SVM模型

clf = svm.LinearSVC() #建立线性SVM分类器
clf.fit(iris.data,iris.target) #用数据训练模型
clf.predict([[5.0,3.6,1.3,0.25]]) #训练好数据后,输入新的数据进行预测
clf.coef_ #查看训练好模型的参数

Output result:
Insert picture description here

Complete code and output:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45154565/article/details/109117868