Thursday, February 25, 2021

Chapter 9 Support Vector Machine within Python - An Introduction to Statistical Learning

Chapter 9 Support Vector Machine - An Introduction to Statistical Learning
In [64]:
%config IPCompleter.greedy=True
%config Completer.use_jedi = False

import pandas as pd
import numpy as np
import random

import seaborn as sns
import matplotlib.pyplot as plt


from sklearn.metrics import confusion_matrix, roc_curve, classification_report, plot_roc_curve, plot_confusion_matrix
from sklearn import svm
from sklearn.model_selection import GridSearchCV
In [2]:
np.random.seed(2)
X=np.random.normal(0,1,size=(20,2))
Y=np.zeros(20)
Y[10:]=1
X[Y==1,:]=X[Y==1,:]+1
df=pd.DataFrame({'Y':Y,'X_1':X[:,0],'X_2':X[:,1]})
#plt.scatter(X[:,0],X[:,1],c=Y)
sns.scatterplot(x='X_1',y='X_2',hue='Y', style='Y',data=df)
Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x2ab08072408>
In [102]:
def summary_svm(df,model):
    display(model.support_vectors_)
    display(model.n_support_)
    if model.kernel=='linear':
        display([model.intercept_]+[model.coef_])
    X=df.loc[:,["X_1","X_2"]].values
    y_pred=model.predict(X)    
    print(classification_report(df.Y.values, y_pred))
    plot_confusion_matrix(model,X,df.Y.values)
    if len(np.unique(df_m.Y.values))==2:
        plot_roc_curve(model,X,df.Y.values)
In [144]:
def plot_decision_boundary(df,model):    
    df=df.astype({'Y':'int'})
    X1_min,X2_min=df.min(axis=0)[1:]-3
    X1_max,X2_max=df.max(axis=0)[1:]+3
    
    x1v,x2v=np.meshgrid(np.linspace(X1_min,X1_max, num=100),np.linspace(X2_min,X2_max,num=100))
    z=model.predict(np.c_[x1v.ravel(),x2v.ravel()])
    z=z.reshape(x1v.shape)    
    plt.contourf(x1v,x2v,z)    
    sns.scatterplot(x='X_1',y='X_2',hue='Y',data=df)
    sns.scatterplot(x=model.support_vectors_[:,0],y=model.support_vectors_[:,1],color='red', marker='+', s=500)
    
    if model.kernel=='linear':
        #W[0]*X+W[1]*Y+Intercept=0
        w = model.coef_[0]
        a = -w[0] / w[1] # slope
        xx = np.linspace(X1_min+1.5, X1_max-2,100)
        # y= WX
        yy = a * xx - (model.intercept_[0]) / w[1]
        # margin =1/ ||W||
        absW=np.sqrt(np.sum(model.coef_ ** 2))
        margin = 1 / absW
        #
        #yy_down = yy - np.sqrt(1 + a ** 2) * margin
        #yy_up = yy + np.sqrt(1 + a ** 2) * margin
        # shift by margin
        distance_to_origin=-model.intercept_[0]/ w[1]/absW
        sh=margin*distance_to_origin
        yy_down = yy - margin
        yy_up = yy + margin
        
        plt.plot(xx, yy, 'r-')
        plt.plot(xx, yy_down, 'k--')
        plt.plot(xx, yy_up, 'k--')    
In [141]:
clf=svm.SVC(kernel='linear', C=1)
clf.fit(X,Y)
Out[141]:
SVC(C=1, kernel='linear')
In [145]:
plot_decision_boundary(df,clf)
summary_svm(df,clf)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [ 0.50288142, -1.24528809],
       [ 0.55145404,  2.29220801],
       [ 0.53905832, -0.5961597 ],
       [-0.0191305 ,  1.17500122],
       [-0.74787095,  0.00902525],
       [ 0.12189211,  0.84356583],
       [ 1.25657045,  0.01122095],
       [ 0.66117803,  0.76381597],
       [ 0.36234499, -0.18761229],
       [-0.42121723,  0.8465048 ],
       [-1.43476758,  1.1127265 ]])
array([7, 6])
[array([-0.27958497]), array([[0.97328421, 0.82834534]])]
              precision    recall  f1-score   support

         0.0       0.80      0.80      0.80        10
         1.0       0.80      0.80      0.80        10

    accuracy                           0.80        20
   macro avg       0.80      0.80      0.80        20
weighted avg       0.80      0.80      0.80        20

In [78]:
clf_10000=svm.SVC(kernel='linear', C=10000)
clf_10000.fit(X,Y)
plot_decision_boundary(df,clf_10000)
summary_svm(df,clf_10000)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [ 0.50288142, -1.24528809],
       [ 0.55145404,  2.29220801],
       [ 0.53905832, -0.5961597 ],
       [-0.0191305 ,  1.17500122],
       [ 0.12189211,  0.84356583],
       [ 1.25657045,  0.01122095],
       [ 0.66117803,  0.76381597],
       [ 0.36234499, -0.18761229],
       [-0.42121723,  0.8465048 ],
       [-1.43476758,  1.1127265 ]])
array([6, 6])
[array([-0.36364199]), array([[1.00330801, 0.91732128]])]
              precision    recall  f1-score   support

         0.0       0.73      0.80      0.76        10
         1.0       0.78      0.70      0.74        10

    accuracy                           0.75        20
   macro avg       0.75      0.75      0.75        20
weighted avg       0.75      0.75      0.75        20

In [71]:
clf_01=svm.SVC(kernel='linear', C=0.01)
clf_01.fit(X,Y)
plot_decision_boundary(df,clf_01)
summary_svm(df,clf_01)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [-1.79343559, -0.84174737],
       [ 0.50288142, -1.24528809],
       [-1.05795222, -0.90900761],
       [ 0.55145404,  2.29220801],
       [ 0.04153939, -1.11792545],
       [ 0.53905832, -0.5961597 ],
       [-0.0191305 ,  1.17500122],
       [-0.74787095,  0.00902525],
       [ 0.12189211,  0.84356583],
       [ 1.25657045,  0.01122095],
       [ 0.66117803,  0.76381597],
       [ 0.36234499, -0.18761229],
       [-0.42121723,  0.8465048 ],
       [ 0.73094304,  3.23136679],
       [-1.43476758,  1.1127265 ],
       [ 1.37044454,  2.35963386],
       [ 1.50185721,  0.1557863 ],
       [ 1.00000976,  1.54235257]])
array([10, 10])
[array([-0.07196016]), array([[0.09685665, 0.10329251]])]
              precision    recall  f1-score   support

         0.0       0.73      0.80      0.76        10
         1.0       0.78      0.70      0.74        10

    accuracy                           0.75        20
   macro avg       0.75      0.75      0.75        20
weighted avg       0.75      0.75      0.75        20

In [73]:
param_grid = {'C':[0.001,0.01,0.1,1,10,100,1000,10000]}
clf_cv=svm.SVC(kernel='linear')

grid_clf=GridSearchCV(estimator=clf_cv,param_grid=param_grid)
grid_clf.fit(X,Y)

means = grid_clf.cv_results_['mean_test_score']
stds = grid_clf.cv_results_['std_test_score']

print(grid_clf.best_params_)
print(pd.DataFrame(grid_clf.cv_results_).set_index('params'))
{'C': 10}
              mean_fit_time  std_fit_time  mean_score_time  std_score_time  \
params                                                                       
{'C': 0.001}       0.000998      0.000632         0.000399        0.000489   
{'C': 0.01}        0.000598      0.000488         0.000399        0.000488   
{'C': 0.1}         0.001206      0.000424         0.000780        0.000390   
{'C': 1}           0.000590      0.000784         0.000592        0.000483   
{'C': 10}          0.000998      0.000005         0.000205        0.000411   
{'C': 100}         0.000597      0.000488         0.000199        0.000398   
{'C': 1000}        0.001394      0.000806         0.000399        0.000488   
{'C': 10000}       0.010970      0.008150         0.000399        0.000489   

             param_C  split0_test_score  split1_test_score  split2_test_score  \
params                                                                          
{'C': 0.001}   0.001               0.50               0.75               0.50   
{'C': 0.01}     0.01               0.50               0.75               0.50   
{'C': 0.1}       0.1               0.50               0.75               0.50   
{'C': 1}           1               0.75               0.75               0.75   
{'C': 10}         10               0.75               0.75               0.75   
{'C': 100}       100               0.75               0.75               0.75   
{'C': 1000}     1000               0.75               0.75               0.75   
{'C': 10000}   10000               0.75               0.75               0.75   

              split3_test_score  split4_test_score  mean_test_score  \
params                                                                
{'C': 0.001}               0.50               0.75             0.60   
{'C': 0.01}                0.50               0.75             0.60   
{'C': 0.1}                 0.50               0.75             0.60   
{'C': 1}                   0.50               0.75             0.70   
{'C': 10}                  0.75               0.75             0.75   
{'C': 100}                 0.75               0.75             0.75   
{'C': 1000}                0.75               0.75             0.75   
{'C': 10000}               0.75               0.75             0.75   

              std_test_score  rank_test_score  
params                                         
{'C': 0.001}        0.122474                6  
{'C': 0.01}         0.122474                6  
{'C': 0.1}          0.122474                6  
{'C': 1}            0.100000                5  
{'C': 10}           0.000000                1  
{'C': 100}          0.000000                1  
{'C': 1000}         0.000000                1  
{'C': 10000}        0.000000                1  
In [79]:
clf_10=svm.SVC(kernel='linear', C=10)
clf_10.fit(X,Y)
plot_decision_boundary(df,clf_10)
summary_svm(df,clf_10)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [ 0.50288142, -1.24528809],
       [ 0.55145404,  2.29220801],
       [ 0.53905832, -0.5961597 ],
       [-0.0191305 ,  1.17500122],
       [ 0.12189211,  0.84356583],
       [ 1.25657045,  0.01122095],
       [ 0.66117803,  0.76381597],
       [ 0.36234499, -0.18761229],
       [-0.42121723,  0.8465048 ],
       [-1.43476758,  1.1127265 ]])
array([6, 6])
[array([-0.36263103]), array([[1.00241093, 0.91678573]])]
              precision    recall  f1-score   support

         0.0       0.73      0.80      0.76        10
         1.0       0.78      0.70      0.74        10

    accuracy                           0.75        20
   macro avg       0.75      0.75      0.75        20
weighted avg       0.75      0.75      0.75        20

In [80]:
X_test=np.random.normal(0,1,size=(20,2))
Y_test=np.zeros(20)
Y_test[10:]=1
X_test[Y_test==1,:]=X_test[Y_test==1,:]+1
df_test=pd.DataFrame({'Y':Y_test,'X_1':X_test[:,0],'X_2':X_test[:,1]})
sns.scatterplot(x='X_1',y='X_2',hue='Y', style='Y',data=df_test)
Out[80]:
<matplotlib.axes._subplots.AxesSubplot at 0x2ab0be0d748>
In [123]:
yHat_test=clf_10.predict(X_test)
confusion_matrix(Y_test,yHat_test)
Out[123]:
array([[5, 5],
       [3, 7]], dtype=int64)
In [84]:
summary_svm(df_test,clf_10)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [ 0.50288142, -1.24528809],
       [ 0.55145404,  2.29220801],
       [ 0.53905832, -0.5961597 ],
       [-0.0191305 ,  1.17500122],
       [ 0.12189211,  0.84356583],
       [ 1.25657045,  0.01122095],
       [ 0.66117803,  0.76381597],
       [ 0.36234499, -0.18761229],
       [-0.42121723,  0.8465048 ],
       [-1.43476758,  1.1127265 ]])
array([6, 6])
              precision    recall  f1-score   support

         0.0       0.62      0.50      0.56        10
         1.0       0.58      0.70      0.64        10

    accuracy                           0.60        20
   macro avg       0.60      0.60      0.60        20
weighted avg       0.60      0.60      0.60        20

In [62]:
display(clf_10.decision_function(X_test))
display(clf_10.predict(X_test))
array([-3.82882591,  1.22268984,  0.25104534, -1.21466403,  0.34651798,
        1.54037013, -1.66619322, -2.22272616, -2.83955358, -2.30525363,
        2.33443856,  2.1306291 ,  0.67025921, -0.74565861, -1.18317973,
        2.0458499 ,  0.57621493,  0.98805065,  2.41374444,  1.0040359 ])
array([0., 1., 1., 0., 1., 1., 0., 0., 0., 0., 1., 1., 1., 0., 0., 1., 1.,
       1., 1., 1.])
In [ ]:
 

Linearly separable

In [36]:
X_lp=X.copy()
X_lp[Y==1,:]=X_lp[Y==1,:]+2
df_lp=pd.DataFrame({'Y':Y,'X_1':X_lp[:,0],'X_2':X_lp[:,1]})
sns.scatterplot(x='X_1',y='X_2',hue='Y', style='Y',data=df_lp)
Out[36]:
<matplotlib.axes._subplots.AxesSubplot at 0x2ab0a676748>
In [81]:
clf_lp=svm.SVC(kernel='linear', C=10)
clf_lp.fit(X_lp,Y)
plot_decision_boundary(df_lp,clf_lp)
summary_svm(df_lp,clf_lp)
array([[0.55145404, 2.29220801],
       [2.36234499, 1.81238771],
       [0.56523242, 3.1127265 ]])
array([1, 2])
[array([-7.47819219]), array([[1.74187943, 2.40733612]])]
              precision    recall  f1-score   support

         0.0       1.00      1.00      1.00        10
         1.0       1.00      1.00      1.00        10

    accuracy                           1.00        20
   macro avg       1.00      1.00      1.00        20
weighted avg       1.00      1.00      1.00        20

In [82]:
summary_svm(df_test,clf_lp)
array([[0.55145404, 2.29220801],
       [2.36234499, 1.81238771],
       [0.56523242, 3.1127265 ]])
array([1, 2])
[array([-7.47819219]), array([[1.74187943, 2.40733612]])]
              precision    recall  f1-score   support

         0.0       0.53      1.00      0.69        10
         1.0       1.00      0.10      0.18        10

    accuracy                           0.55        20
   macro avg       0.76      0.55      0.44        20
weighted avg       0.76      0.55      0.44        20

Support Vector Machine with Kernel

In [83]:
clf_nl_rd=svm.SVC(kernel='rbf', C=10, gamma=0.5)
clf_nl_rd.fit(X,Y)
plot_decision_boundary(df,clf_nl_rd)
summary_svm(df,clf_nl_rd)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [ 0.55145404,  2.29220801],
       [ 0.53905832, -0.5961597 ],
       [-0.0191305 ,  1.17500122],
       [-0.74787095,  0.00902525],
       [ 0.12189211,  0.84356583],
       [ 0.36234499, -0.18761229],
       [-0.42121723,  0.8465048 ],
       [ 0.73094304,  3.23136679],
       [-1.43476758,  1.1127265 ],
       [ 1.37044454,  2.35963386],
       [ 1.00000976,  1.54235257]])
array([6, 7])
              precision    recall  f1-score   support

         0.0       0.90      0.90      0.90        10
         1.0       0.90      0.90      0.90        10

    accuracy                           0.90        20
   macro avg       0.90      0.90      0.90        20
weighted avg       0.90      0.90      0.90        20

In [146]:
clf_nl_pol2=svm.SVC(kernel='poly',degree=2, C=1)
clf_nl_pol2.fit(X,Y)
plot_decision_boundary(df,clf_nl_pol2)
summary_svm(df,clf_nl_pol2)
array([[-0.41675785, -0.05626683],
       [-2.1361961 ,  1.64027081],
       [-1.79343559, -0.84174737],
       [ 0.50288142, -1.24528809],
       [-1.05795222, -0.90900761],
       [ 0.55145404,  2.29220801],
       [ 0.04153939, -1.11792545],
       [ 0.53905832, -0.5961597 ],
       [-0.0191305 ,  1.17500122],
       [-0.74787095,  0.00902525],
       [ 0.12189211,  0.84356583],
       [ 1.25657045,  0.01122095],
       [ 0.66117803,  0.76381597],
       [ 0.36234499, -0.18761229],
       [-0.42121723,  0.8465048 ],
       [ 0.73094304,  3.23136679],
       [-1.43476758,  1.1127265 ],
       [ 1.37044454,  2.35963386],
       [ 1.50185721,  0.1557863 ],
       [ 1.00000976,  1.54235257]])
array([10, 10])
              precision    recall  f1-score   support

         0.0       0.50      0.80      0.62        10
         1.0       0.50      0.20      0.29        10

    accuracy                           0.50        20
   macro avg       0.50      0.50      0.45        20
weighted avg       0.50      0.50      0.45        20

In [59]:
param_rbf_grid = {'C':[0.001,0.01,0.1,1,10,100,1000,10000], 'gamma':[0.5,1,2,3,4]}
clf_cv_rbf=svm.SVC(kernel='rbf')

grid_clf_rbf=GridSearchCV(estimator=clf_cv_rbf,param_grid=param_rbf_grid)
grid_clf_rbf.fit(X,Y)

means = grid_clf_rbf.cv_results_['mean_test_score']
stds = grid_clf_rbf.cv_results_['std_test_score']

print(grid_clf_rbf.best_params_)
print(pd.DataFrame(grid_clf_rbf.cv_results_).set_index('params'))
{'C': 10, 'gamma': 0.5}
                            mean_fit_time  std_fit_time  mean_score_time  \
params                                                                     
{'C': 0.001, 'gamma': 0.5}       0.001007      0.000012         0.000399   
{'C': 0.001, 'gamma': 1}         0.000983      0.000012         0.000204   
{'C': 0.001, 'gamma': 2}         0.000389      0.000477         0.000399   
{'C': 0.001, 'gamma': 3}         0.000401      0.000491         0.000600   
{'C': 0.001, 'gamma': 4}         0.000610      0.000498         0.000395   
{'C': 0.01, 'gamma': 0.5}        0.000398      0.000487         0.000396   
{'C': 0.01, 'gamma': 1}          0.000596      0.000487         0.000399   
{'C': 0.01, 'gamma': 2}          0.000780      0.000390         0.000591   
{'C': 0.01, 'gamma': 3}          0.000192      0.000385         0.000598   
{'C': 0.01, 'gamma': 4}          0.000598      0.000489         0.000399   
{'C': 0.1, 'gamma': 0.5}         0.000200      0.000399         0.000797   
{'C': 0.1, 'gamma': 1}           0.000000      0.000000         0.000192   
{'C': 0.1, 'gamma': 2}           0.000200      0.000401         0.000199   
{'C': 0.1, 'gamma': 3}           0.000000      0.000000         0.000000   
{'C': 0.1, 'gamma': 4}           0.000192      0.000383         0.000000   
{'C': 1, 'gamma': 0.5}           0.001005      0.000015         0.000000   
{'C': 1, 'gamma': 1}             0.000197      0.000394         0.000399   
{'C': 1, 'gamma': 2}             0.000606      0.000495         0.000391   
{'C': 1, 'gamma': 3}             0.000790      0.000396         0.000000   
{'C': 1, 'gamma': 4}             0.000000      0.000000         0.000000   
{'C': 10, 'gamma': 0.5}          0.000000      0.000000         0.001002   
{'C': 10, 'gamma': 1}            0.000797      0.000399         0.000200   
{'C': 10, 'gamma': 2}            0.000591      0.000483         0.000404   
{'C': 10, 'gamma': 3}            0.000000      0.000000         0.000391   
{'C': 10, 'gamma': 4}            0.000207      0.000413         0.000805   
{'C': 100, 'gamma': 0.5}         0.000997      0.000024         0.000000   
{'C': 100, 'gamma': 1}           0.000207      0.000414         0.000000   
{'C': 100, 'gamma': 2}           0.000200      0.000400         0.000192   
{'C': 100, 'gamma': 3}           0.000805      0.000403         0.000199   
{'C': 100, 'gamma': 4}           0.000195      0.000390         0.000000   
{'C': 1000, 'gamma': 0.5}        0.000000      0.000000         0.000798   
{'C': 1000, 'gamma': 1}          0.000192      0.000384         0.000598   
{'C': 1000, 'gamma': 2}          0.000000      0.000000         0.000192   
{'C': 1000, 'gamma': 3}          0.000598      0.000489         0.000406   
{'C': 1000, 'gamma': 4}          0.000399      0.000488         0.000200   
{'C': 10000, 'gamma': 0.5}       0.000255      0.000386         0.000545   
{'C': 10000, 'gamma': 1}         0.000195      0.000389         0.000799   
{'C': 10000, 'gamma': 2}         0.000400      0.000491         0.000000   
{'C': 10000, 'gamma': 3}         0.000797      0.000399         0.000200   
{'C': 10000, 'gamma': 4}         0.000399      0.000489         0.000000   

                            std_score_time param_C param_gamma  \
params                                                           
{'C': 0.001, 'gamma': 0.5}        0.000488   0.001         0.5   
{'C': 0.001, 'gamma': 1}          0.000409   0.001           1   
{'C': 0.001, 'gamma': 2}          0.000488   0.001           2   
{'C': 0.001, 'gamma': 3}          0.000490   0.001           3   
{'C': 0.001, 'gamma': 4}          0.000484   0.001           4   
{'C': 0.01, 'gamma': 0.5}         0.000485    0.01         0.5   
{'C': 0.01, 'gamma': 1}           0.000489    0.01           1   
{'C': 0.01, 'gamma': 2}           0.000483    0.01           2   
{'C': 0.01, 'gamma': 3}           0.000489    0.01           3   
{'C': 0.01, 'gamma': 4}           0.000489    0.01           4   
{'C': 0.1, 'gamma': 0.5}          0.000399     0.1         0.5   
{'C': 0.1, 'gamma': 1}            0.000383     0.1           1   
{'C': 0.1, 'gamma': 2}            0.000399     0.1           2   
{'C': 0.1, 'gamma': 3}            0.000000     0.1           3   
{'C': 0.1, 'gamma': 4}            0.000000     0.1           4   
{'C': 1, 'gamma': 0.5}            0.000000       1         0.5   
{'C': 1, 'gamma': 1}              0.000489       1           1   
{'C': 1, 'gamma': 2}              0.000479       1           2   
{'C': 1, 'gamma': 3}              0.000000       1           3   
{'C': 1, 'gamma': 4}              0.000000       1           4   
{'C': 10, 'gamma': 0.5}           0.000011      10         0.5   
{'C': 10, 'gamma': 1}             0.000400      10           1   
{'C': 10, 'gamma': 2}             0.000495      10           2   
{'C': 10, 'gamma': 3}             0.000479      10           3   
{'C': 10, 'gamma': 4}             0.000403      10           4   
{'C': 100, 'gamma': 0.5}          0.000000     100         0.5   
{'C': 100, 'gamma': 1}            0.000000     100           1   
{'C': 100, 'gamma': 2}            0.000383     100           2   
{'C': 100, 'gamma': 3}            0.000399     100           3   
{'C': 100, 'gamma': 4}            0.000000     100           4   
{'C': 1000, 'gamma': 0.5}         0.000400    1000         0.5   
{'C': 1000, 'gamma': 1}           0.000489    1000           1   
{'C': 1000, 'gamma': 2}           0.000384    1000           2   
{'C': 1000, 'gamma': 3}           0.000498    1000           3   
{'C': 1000, 'gamma': 4}           0.000399    1000           4   
{'C': 10000, 'gamma': 0.5}        0.000454   10000         0.5   
{'C': 10000, 'gamma': 1}          0.000399   10000           1   
{'C': 10000, 'gamma': 2}          0.000000   10000           2   
{'C': 10000, 'gamma': 3}          0.000400   10000           3   
{'C': 10000, 'gamma': 4}          0.000000   10000           4   

                            split0_test_score  split1_test_score  \
params                                                             
{'C': 0.001, 'gamma': 0.5}               0.75               0.75   
{'C': 0.001, 'gamma': 1}                 0.75               0.75   
{'C': 0.001, 'gamma': 2}                 0.75               0.75   
{'C': 0.001, 'gamma': 3}                 0.75               0.75   
{'C': 0.001, 'gamma': 4}                 0.75               0.75   
{'C': 0.01, 'gamma': 0.5}                0.75               0.75   
{'C': 0.01, 'gamma': 1}                  0.75               0.75   
{'C': 0.01, 'gamma': 2}                  0.75               0.75   
{'C': 0.01, 'gamma': 3}                  0.75               0.75   
{'C': 0.01, 'gamma': 4}                  0.75               0.75   
{'C': 0.1, 'gamma': 0.5}                 0.75               0.75   
{'C': 0.1, 'gamma': 1}                   0.75               0.75   
{'C': 0.1, 'gamma': 2}                   0.75               0.75   
{'C': 0.1, 'gamma': 3}                   0.75               0.75   
{'C': 0.1, 'gamma': 4}                   0.75               0.75   
{'C': 1, 'gamma': 0.5}                   0.75               0.75   
{'C': 1, 'gamma': 1}                     0.75               0.75   
{'C': 1, 'gamma': 2}                     0.75               0.75   
{'C': 1, 'gamma': 3}                     0.75               0.75   
{'C': 1, 'gamma': 4}                     0.75               0.75   
{'C': 10, 'gamma': 0.5}                  0.75               0.75   
{'C': 10, 'gamma': 1}                    0.75               0.75   
{'C': 10, 'gamma': 2}                    0.75               0.75   
{'C': 10, 'gamma': 3}                    0.50               0.75   
{'C': 10, 'gamma': 4}                    0.50               0.75   
{'C': 100, 'gamma': 0.5}                 0.75               1.00   
{'C': 100, 'gamma': 1}                   0.75               0.75   
{'C': 100, 'gamma': 2}                   0.75               0.75   
{'C': 100, 'gamma': 3}                   0.50               0.75   
{'C': 100, 'gamma': 4}                   0.50               0.75   
{'C': 1000, 'gamma': 0.5}                0.75               1.00   
{'C': 1000, 'gamma': 1}                  0.75               0.75   
{'C': 1000, 'gamma': 2}                  0.75               0.75   
{'C': 1000, 'gamma': 3}                  0.50               0.75   
{'C': 1000, 'gamma': 4}                  0.50               0.75   
{'C': 10000, 'gamma': 0.5}               0.75               1.00   
{'C': 10000, 'gamma': 1}                 0.75               0.75   
{'C': 10000, 'gamma': 2}                 0.75               0.75   
{'C': 10000, 'gamma': 3}                 0.50               0.75   
{'C': 10000, 'gamma': 4}                 0.50               0.75   

                            split2_test_score  split3_test_score  \
params                                                             
{'C': 0.001, 'gamma': 0.5}               0.50               0.50   
{'C': 0.001, 'gamma': 1}                 0.50               0.50   
{'C': 0.001, 'gamma': 2}                 0.50               0.25   
{'C': 0.001, 'gamma': 3}                 0.25               0.25   
{'C': 0.001, 'gamma': 4}                 0.25               0.25   
{'C': 0.01, 'gamma': 0.5}                0.50               0.50   
{'C': 0.01, 'gamma': 1}                  0.50               0.50   
{'C': 0.01, 'gamma': 2}                  0.50               0.25   
{'C': 0.01, 'gamma': 3}                  0.25               0.25   
{'C': 0.01, 'gamma': 4}                  0.25               0.25   
{'C': 0.1, 'gamma': 0.5}                 0.50               0.50   
{'C': 0.1, 'gamma': 1}                   0.50               0.50   
{'C': 0.1, 'gamma': 2}                   0.50               0.25   
{'C': 0.1, 'gamma': 3}                   0.25               0.25   
{'C': 0.1, 'gamma': 4}                   0.25               0.25   
{'C': 1, 'gamma': 0.5}                   0.50               0.75   
{'C': 1, 'gamma': 1}                     0.50               0.75   
{'C': 1, 'gamma': 2}                     0.50               0.25   
{'C': 1, 'gamma': 3}                     0.50               0.25   
{'C': 1, 'gamma': 4}                     0.50               0.25   
{'C': 10, 'gamma': 0.5}                  1.00               0.50   
{'C': 10, 'gamma': 1}                    0.50               0.75   
{'C': 10, 'gamma': 2}                    0.50               0.25   
{'C': 10, 'gamma': 3}                    0.50               0.25   
{'C': 10, 'gamma': 4}                    0.50               0.25   
{'C': 100, 'gamma': 0.5}                 0.50               0.75   
{'C': 100, 'gamma': 1}                   0.50               0.75   
{'C': 100, 'gamma': 2}                   0.50               0.25   
{'C': 100, 'gamma': 3}                   0.50               0.25   
{'C': 100, 'gamma': 4}                   0.50               0.25   
{'C': 1000, 'gamma': 0.5}                0.50               0.75   
{'C': 1000, 'gamma': 1}                  0.50               0.75   
{'C': 1000, 'gamma': 2}                  0.50               0.25   
{'C': 1000, 'gamma': 3}                  0.50               0.25   
{'C': 1000, 'gamma': 4}                  0.50               0.25   
{'C': 10000, 'gamma': 0.5}               0.50               0.75   
{'C': 10000, 'gamma': 1}                 0.50               0.75   
{'C': 10000, 'gamma': 2}                 0.50               0.25   
{'C': 10000, 'gamma': 3}                 0.50               0.25   
{'C': 10000, 'gamma': 4}                 0.50               0.25   

                            split4_test_score  mean_test_score  \
params                                                           
{'C': 0.001, 'gamma': 0.5}               0.75             0.65   
{'C': 0.001, 'gamma': 1}                 0.75             0.65   
{'C': 0.001, 'gamma': 2}                 0.75             0.60   
{'C': 0.001, 'gamma': 3}                 0.75             0.55   
{'C': 0.001, 'gamma': 4}                 0.75             0.55   
{'C': 0.01, 'gamma': 0.5}                0.75             0.65   
{'C': 0.01, 'gamma': 1}                  0.75             0.65   
{'C': 0.01, 'gamma': 2}                  0.75             0.60   
{'C': 0.01, 'gamma': 3}                  0.75             0.55   
{'C': 0.01, 'gamma': 4}                  0.75             0.55   
{'C': 0.1, 'gamma': 0.5}                 0.75             0.65   
{'C': 0.1, 'gamma': 1}                   0.75             0.65   
{'C': 0.1, 'gamma': 2}                   0.75             0.60   
{'C': 0.1, 'gamma': 3}                   0.75             0.55   
{'C': 0.1, 'gamma': 4}                   0.75             0.55   
{'C': 1, 'gamma': 0.5}                   0.75             0.70   
{'C': 1, 'gamma': 1}                     0.75             0.70   
{'C': 1, 'gamma': 2}                     0.75             0.60   
{'C': 1, 'gamma': 3}                     0.75             0.60   
{'C': 1, 'gamma': 4}                     0.75             0.60   
{'C': 10, 'gamma': 0.5}                  0.75             0.75   
{'C': 10, 'gamma': 1}                    0.50             0.65   
{'C': 10, 'gamma': 2}                    0.75             0.60   
{'C': 10, 'gamma': 3}                    0.75             0.55   
{'C': 10, 'gamma': 4}                    0.75             0.55   
{'C': 100, 'gamma': 0.5}                 0.75             0.75   
{'C': 100, 'gamma': 1}                   0.50             0.65   
{'C': 100, 'gamma': 2}                   0.75             0.60   
{'C': 100, 'gamma': 3}                   0.75             0.55   
{'C': 100, 'gamma': 4}                   0.75             0.55   
{'C': 1000, 'gamma': 0.5}                0.75             0.75   
{'C': 1000, 'gamma': 1}                  0.50             0.65   
{'C': 1000, 'gamma': 2}                  0.75             0.60   
{'C': 1000, 'gamma': 3}                  0.75             0.55   
{'C': 1000, 'gamma': 4}                  0.75             0.55   
{'C': 10000, 'gamma': 0.5}               0.75             0.75   
{'C': 10000, 'gamma': 1}                 0.50             0.65   
{'C': 10000, 'gamma': 2}                 0.75             0.60   
{'C': 10000, 'gamma': 3}                 0.75             0.55   
{'C': 10000, 'gamma': 4}                 0.75             0.55   

                            std_test_score  rank_test_score  
params                                                       
{'C': 0.001, 'gamma': 0.5}        0.122474                7  
{'C': 0.001, 'gamma': 1}          0.122474                7  
{'C': 0.001, 'gamma': 2}          0.200000               17  
{'C': 0.001, 'gamma': 3}          0.244949               27  
{'C': 0.001, 'gamma': 4}          0.244949               27  
{'C': 0.01, 'gamma': 0.5}         0.122474                7  
{'C': 0.01, 'gamma': 1}           0.122474                7  
{'C': 0.01, 'gamma': 2}           0.200000               17  
{'C': 0.01, 'gamma': 3}           0.244949               27  
{'C': 0.01, 'gamma': 4}           0.244949               27  
{'C': 0.1, 'gamma': 0.5}          0.122474                7  
{'C': 0.1, 'gamma': 1}            0.122474                7  
{'C': 0.1, 'gamma': 2}            0.200000               17  
{'C': 0.1, 'gamma': 3}            0.244949               27  
{'C': 0.1, 'gamma': 4}            0.244949               27  
{'C': 1, 'gamma': 0.5}            0.100000                5  
{'C': 1, 'gamma': 1}              0.100000                5  
{'C': 1, 'gamma': 2}              0.200000               17  
{'C': 1, 'gamma': 3}              0.200000               17  
{'C': 1, 'gamma': 4}              0.200000               17  
{'C': 10, 'gamma': 0.5}           0.158114                1  
{'C': 10, 'gamma': 1}             0.122474                7  
{'C': 10, 'gamma': 2}             0.200000               17  
{'C': 10, 'gamma': 3}             0.187083               27  
{'C': 10, 'gamma': 4}             0.187083               27  
{'C': 100, 'gamma': 0.5}          0.158114                1  
{'C': 100, 'gamma': 1}            0.122474                7  
{'C': 100, 'gamma': 2}            0.200000               17  
{'C': 100, 'gamma': 3}            0.187083               27  
{'C': 100, 'gamma': 4}            0.187083               27  
{'C': 1000, 'gamma': 0.5}         0.158114                1  
{'C': 1000, 'gamma': 1}           0.122474                7  
{'C': 1000, 'gamma': 2}           0.200000               17  
{'C': 1000, 'gamma': 3}           0.187083               27  
{'C': 1000, 'gamma': 4}           0.187083               27  
{'C': 10000, 'gamma': 0.5}        0.158114                1  
{'C': 10000, 'gamma': 1}          0.122474                7  
{'C': 10000, 'gamma': 2}          0.200000               17  
{'C': 10000, 'gamma': 3}          0.187083               27  
{'C': 10000, 'gamma': 4}          0.187083               27  

ROC Curves

In [72]:
plot_roc_curve(clf_nl_rd,X,Y)
Out[72]:
<sklearn.metrics._plot.roc_curve.RocCurveDisplay at 0x2ab0bd04308>

SVM with Multiple Classes

In [147]:
np.random.seed(123)
X_m=np.random.normal(0,1,size=(50,2))
Y_m=np.zeros(50)
Y_m[:20]=0
Y_m[20:35]=1
Y_m[35:]=2
X_m[Y_m==1,:]=X_m[Y_m==1,:]+1
X_m[Y_m==2,:]=X_m[Y_m==2,:]+2

df_m=pd.DataFrame({'Y':Y_m,'X_1':X_m[:,0],'X_2':X_m[:,1]})
#plt.scatter(X_m[:,0],X_m[:,1],c=Y_m)
#clazzes=np.array(np.select([Y_m==0,Y_m==1,Y_m==2],['r','b','g']))
sns.scatterplot(x='X_1',y='X_2',hue=Y_m.astype('int'),data=df_m)
Out[147]:
<matplotlib.axes._subplots.AxesSubplot at 0x2ab0d11bd48>
In [152]:
clf_m=svm.SVC(kernel='rbf', decision_function_shape='ovo', gamma=0.5, C=10)
clf_m.fit(X_m,Y_m)
plot_decision_boundary(df_m,clf_m)
summary_svm(df_m,clf_m)
array([[-1.08563060e+00,  9.97345447e-01],
       [-5.78600252e-01,  1.65143654e+00],
       [-2.42667924e+00, -4.28912629e-01],
       [-6.78886152e-01, -9.47089689e-02],
       [ 1.49138963e+00, -6.38901997e-01],
       [-4.43981960e-01, -4.34351276e-01],
       [ 2.20593008e+00,  2.18678609e+00],
       [ 1.00405390e+00,  3.86186399e-01],
       [ 7.37368576e-01,  1.49073203e+00],
       [-1.25388067e+00, -6.37751502e-01],
       [ 9.07105196e-01, -1.42868070e+00],
       [-1.40068720e-01, -8.61754896e-01],
       [-2.55619371e-01, -2.79858911e+00],
       [-1.77153310e+00, -6.99877235e-01],
       [ 9.27462432e-01, -1.73635683e-01],
       [ 2.84591590e-03,  6.88222711e-01],
       [ 1.94633482e-01, -7.27669494e-01],
       [ 6.09100206e-01,  1.57380586e+00],
       [ 1.33858905e+00,  9.88169506e-01],
       [ 3.39236527e+00,  1.41291216e+00],
       [ 1.97873601e+00,  3.23814334e+00],
       [-2.94085323e-01, -3.87882102e-02],
       [ 2.74371223e+00,  2.01937265e-01],
       [ 1.02968323e+00,  2.06931597e+00],
       [ 1.89070639e+00,  2.75488618e+00],
       [ 2.49564414e+00,  2.06939267e+00],
       [ 2.27291286e-01,  1.79486267e+00],
       [ 1.31427199e+00, -3.26265460e-01],
       [ 2.41729905e+00,  1.80723653e+00],
       [ 1.04549008e+00,  7.66907939e-01],
       [-1.98301145e-01,  1.19952407e+00],
       [ 2.46843912e+00,  1.16884502e+00],
       [-1.23100350e-01,  3.03972709e+00],
       [ 1.59663396e+00,  1.87397041e+00],
       [ 1.16248328e+00,  3.94037239e-01],
       [ 3.25523737e+00,  1.31113102e+00],
       [ 3.66095249e+00,  2.80730819e+00],
       [ 1.68524185e+00,  9.14097599e-01],
       [ 1.26753801e+00,  7.87476869e-01],
       [ 4.08711336e+00,  2.16444123e+00],
       [ 3.15020554e+00,  7.32647951e-01],
       [ 2.18103513e+00,  3.17786194e+00],
       [ 1.66498924e+00,  3.03111446e+00],
       [ 9.15432088e-01,  6.36528455e-01],
       [ 2.37940061e+00,  1.62082357e+00]])
array([16, 15, 14])
              precision    recall  f1-score   support

         0.0       0.81      0.85      0.83        20
         1.0       0.62      0.53      0.57        15
         2.0       0.75      0.80      0.77        15

    accuracy                           0.74        50
   macro avg       0.72      0.73      0.72        50
weighted avg       0.73      0.74      0.74        50

Application to Gene Expression Data

In [173]:
df_k_xtrain=pd.read_csv('data/Khan_xtrain.csv', index_col=0)
df_k_xtest=pd.read_csv('data/Khan_xtest.csv', index_col=0)
df_k_ytrain=pd.read_csv('data/Khan_ytrain.csv', index_col=0,dtype='int')
df_k_ytest=pd.read_csv('data/Khan_ytest.csv', index_col=0,dtype='int')
In [174]:
display(df_k_xtrain.info())
display(df_k_xtest.info())
display(df_k_ytrain.info())
display(df_k_ytest.info())
<class 'pandas.core.frame.DataFrame'>
Index: 63 entries, V1 to V63
Columns: 2308 entries, V1 to V2308
dtypes: float64(2308)
memory usage: 1.1+ MB
None
<class 'pandas.core.frame.DataFrame'>
Index: 20 entries, V1 to V25
Columns: 2308 entries, V1 to V2308
dtypes: float64(2308)
memory usage: 360.8+ KB
None
<class 'pandas.core.frame.DataFrame'>
Int64Index: 63 entries, 1 to 63
Data columns (total 1 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   x       63 non-null     int32
dtypes: int32(1)
memory usage: 756.0 bytes
None
<class 'pandas.core.frame.DataFrame'>
Int64Index: 20 entries, 1 to 20
Data columns (total 1 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   x       20 non-null     int32
dtypes: int32(1)
memory usage: 240.0 bytes
None
In [178]:
df_k_xtrain.head()
df_k_xtest.head()
df_k_ytest.head()
df_k_ytrain.head()
Out[178]:
x
1 2
2 2
3 2
4 2
5 2
In [200]:
clf_k=svm.SVC(kernel='linear',C=10, decision_function_shape='ovo')
clf_k.fit(df_k_xtrain.values,df_k_ytrain.values)
D:\apps\Anaconda3\envs\dev\lib\site-packages\sklearn\utils\validation.py:73: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  return f(**kwargs)
Out[200]:
SVC(C=10, decision_function_shape='ovo', kernel='linear')
In [201]:
y_k_pred=clf_k.predict(df_k_xtest)    
print(classification_report(df_k_ytest.values, y_k_pred))
              precision    recall  f1-score   support

           1       1.00      1.00      1.00         3
           2       0.75      1.00      0.86         6
           3       1.00      0.67      0.80         6
           4       1.00      1.00      1.00         5

    accuracy                           0.90        20
   macro avg       0.94      0.92      0.91        20
weighted avg       0.93      0.90      0.90        20

In [202]:
print(plot_confusion_matrix(clf_k,df_k_xtest.values,df_k_ytest.values))
<sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay object at 0x000002AB4AD37708>
In [ ]:
 

No comments: