Machine Learning Notes (12) --- Using SVM in Sklearn

The svm theory is too difficult to understand. Let’s improve the confidence of the SVM code in the last sklearn, and the theory will be added later.

import numpy as np
from sklearn import datasets
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC

def sklearn_svm_test():
    iris = datasets.load_iris()#加载sklearn自带的鸢尾花数据。一共150条数据,分三类,每类50条数据,这三类用0,1,2表示,分别表示:setosa, versicolor, virginica
    #第一条数据特征分别表示:花萼长度, 花萼宽度,花瓣长度, 花瓣宽度.
    X = iris["data"][:, (2,3)]#取"data"第行的第3和第4列,即取的花瓣长度和花瓣宽度。
    y = (iris["target"] == 2).astype( np.float64 )
    #"target"中label为2的值转成float64类型(原来是整形)。
    #另外注意是把满足条件的位置置为1.0,其它位置为0.0

    svm_clf = Pipeline(( ("scaler", StandardScaler()),
                         ("linear_svc", LinearSVC(C=1, loss="hinge")) ,))

    #X,y都是ndarray类型。
    #X的数据长这样:
    # [[ 1.4  0.2]
    # [ 1.4  0.2]
    # [ 1.3  0.2]
    # [ 1.5  0.2]
    # [ 1.4  0.2]
    # [ 1.7  0.4]
    # [ 1.4  0.3]
    # [ 1.5  0.2]
    # [ 1.4  0.2]
    # [ 1.5  0.1]
    # [……       ]]

    #y的数据长这样:
    # [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
    # 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
    # 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
    # 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
    # 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
    # 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  1.  1.  1.  1.  1.  1.  1.
    # 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
    # 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.
    # 1.  1.  1.  1.  1.  1.]

    svm_clf.fit( X, y )
    res = svm_clf.predict( [[1.3, 2.6]] )

    print(res)

if __name__ == '__main__':
    #使用sklearn中的SVM算法
    sklearn_svm_test()

Therefore, after learning so far, I found that it is very simple to use the algorithm of machine learning. You can directly call the relevant interface in sklearn. At most, you can change the parameters to see the effect of different algorithms. It is difficult to understand the background. Theoretical basis, as well as the acquisition of positive and negative samples and the selection of features.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854173&siteId=291194637