Sklearn of SVM

'' ' 
    SVM: 
        Support Vector Machine principle: 
            the classification principles: finding the optimal classification boundary 
                1. correct: For most samples can be divided into categories correctly. 
                2. generalization: to maximize support vector spacing. 
                3. Equity: support vector isometric. 
                4. Simple: linear, line or plane, dividing hyperplane. 

        Based liter dimensional transform kernel function: characterized by a kernel function called conversion, adding new features, such that the linear low-dimensional space can not be separated to a high dimensional space the problem of linearly separable problem. 

            1> linear kernel: linear, not by nuclear dimension to enhance the function, only seek linear classification boundary in the original dimensions of space. 

            2> polynomial kernel: poly, characterized by increasing the original sample high - power polynomial function 
                    Y = x_1 + x_2 
                    Y = x_1 + x_2 ^ 2 ^ 2 + 2x_1x_2 
                    Y = x_1 ^ ^ 2x_2 3x_1. 3 + 2 + ^ + 3x_1x_2 ^. 3 x_2 

            . 3> kernel function: rbf, by increasing the probability distribution function of the Gaussian distribution characteristics of the original sample 

        based on the linear kernel SVM classification related the API: 
                Model = svm.SVC (Kernel = 'linear'
                model.fit (train_x, train_y) 

        cases, data based on the radial basis function in sample2.txt training samples. 
            Steps: 
                1. Read the file, the distribution of sample points plotted 
                2. Split Test set Training set 
                3. svm train the classification model based 
                4. Output classification results, the boundary drawing classification 
'' ' 
Import numpy AS NP
 Import sklearn.model_selection AS MS
 Import sklearn.svm AS SVM
 Import sklearn.metrics AS SM
 Import matplotlib.pyplot AS MP 

Data = np.loadtxt ( ' ./ml_data/multiple2.txt ' , DELIMITER = ' , ' , the unpack = False, DTYPE = ' F8 ') 
X = Data [:,: -1] 
Y = Data [:, -1 ] 

# Caifen training set and test set 
train_x, test_x, train_y, test_y ms.train_test_split = (X, Y, test_size = 0.25, random_state =. 5 ) 

# training svm Based Model --- linear kernel 
# model = svm.SVC (Kernel = 'linear') 
# model.fit (train_x, train_y) 

# training svm model based on polynomial kernel --- 
# model = svm.SVC (Kernel = 'poly', Degree . 3 =) 
# model.fit (train_x, train_y) 

# training model svm --- polynomial kernel based 
model = svm.SVC (Kernel = ' RBF ' , C = 600 ) 
model.fit (train_x, train_y) 

# forecast 
pred_test_y =model.predict (test_x) 

# calculation model accuracy 
BG = sm.classification_report (test_y, pred_test_y)
 Print ( ' Category Report: ' , BG, On Sep = ' \ n- ' ) 

# draw the boundary line classification 
l, r = x [:, 0] .min () -. 1, X [0 :,] .max () +. 1 
B, T = X [:,. 1] .min () -. 1, X [:,. 1] .max () +. 1 
n- = 500 
grid_x, grid_y = np.meshgrid (np.linspace (L, R & lt, n-), np.linspace (B, T, n-)) 
bg_x = np.column_stack ((grid_x.ravel (), grid_y.ravel ( ))) 
bg_y = model.predict (bg_x) 
grid_z = bg_y.reshape (grid_x.shape)

 # drawing shows a sample data
mp.figure('SVM Classification', facecolor='lightgray')
mp.title('SVM Classification', fontsize=16)
mp.xlabel('X', fontsize=14)
mp.ylabel('Y', fontsize=14)
mp.tick_params(labelsize=10)
mp.pcolormesh(grid_x, grid_y, grid_z, cmap='gray')
mp.scatter(test_x[:, 0], test_x[:, 1], s=80, c=test_y, cmap='jet', Label =' The Samples ' ) 

mp.legend () 
() mp.show 



output: 
Category Report: 
              Precision Recall F1 - Score Support

          0.0 0.91 0.87 0.89 45 
         1.0 30 0.81 0.87 0.84 

    Accuracy                            0.87 75 
   Macro AVG        0.86 0.87 0.86 75 
Weighted AVG        0.87 0.87 0.87 75

  

 

  

 

  

Guess you like

Origin www.cnblogs.com/yuxiangyang/p/11198695.html
svm