In [2]:
import pandas as pd
import numpy as np
import joblib
from sklearn.svm import SVC
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

Read Data


In [3]:
data = pd.read_csv("data/driver_image.csv")
data = data.ix[:,1:]
X_data = data.ix[:,:-1]
y_data = data.ix[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.25)

Support Vector Machine(Linear)


In [ ]:
%%time
svc_linear = SVC(kernel='linear').fit(X_train, y_train)

In [4]:
joblib.dump(svc_linear, "model/svc_linear") #save model


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-f991f45030fe> in <module>()
----> 1 joblib.dump(svc_linear, "model/svc_linear") #save model

NameError: name 'svc_linear' is not defined

In [5]:
svc_linear = joblib.load("model/svc_linear")

In [6]:
%%time
linear_predict = svc_linear.predict(X_test)


Wall time: 3min 31s

In [7]:
print(classification_report(y_test, linear_predict))


             precision    recall  f1-score   support

        0.0       0.99      1.00      0.99       643
        1.0       1.00      1.00      1.00       579
        2.0       0.99      1.00      1.00       555
        3.0       1.00      1.00      1.00       620
        4.0       1.00      0.99      1.00       582
        5.0       1.00      0.99      1.00       561
        6.0       1.00      1.00      1.00       574
        7.0       1.00      1.00      1.00       506
        8.0       1.00      0.99      0.99       475
        9.0       0.99      0.99      0.99       511

avg / total       1.00      1.00      1.00      5606


In [8]:
confusion_matrix(y_test, linear_predict)


Out[8]:
array([[642,   0,   0,   0,   0,   0,   1,   0,   0,   0],
       [  1, 578,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0, 555,   0,   0,   0,   0,   0,   0,   0],
       [  1,   0,   0, 619,   0,   0,   0,   0,   0,   0],
       [  3,   0,   1,   0, 578,   0,   0,   0,   0,   0],
       [  2,   0,   0,   0,   0, 557,   0,   0,   1,   1],
       [  0,   0,   1,   0,   0,   0, 573,   0,   0,   0],
       [  0,   0,   1,   0,   1,   0,   0, 504,   0,   0],
       [  1,   0,   1,   0,   0,   0,   0,   0, 471,   2],
       [  1,   1,   0,   0,   0,   1,   0,   0,   1, 507]])

In [9]:
sum(y_test != linear_predict)


Out[9]:
22

Kernel Support Vector Machine (RBF)


In [ ]:
from sklearn.svm import SVC

In [36]:
%%time
svc_rbf = SVC(kernel='rbf', gamma=0.00000009).fit(X_train, y_train)

In [37]:
joblib.dump(svc_rbf, "model/svc_rbf") #save model


Out[37]:
['model/svc_rbf',
 'model/svc_rbf_01.npy',
 'model/svc_rbf_02.npy',
 'model/svc_rbf_03.npy',
 'model/svc_rbf_04.npy',
 'model/svc_rbf_05.npy',
 'model/svc_rbf_06.npy',
 'model/svc_rbf_07.npy',
 'model/svc_rbf_08.npy',
 'model/svc_rbf_09.npy',
 'model/svc_rbf_10.npy',
 'model/svc_rbf_11.npy']

In [10]:
svc_rbf = joblib.load("model/svc_rbf")

In [11]:
%%time
rbf_predict = svc_rbf.predict(X_test)


Wall time: 4min 25s

In [12]:
print(classification_report(y_test, rbf_predict))


             precision    recall  f1-score   support

        0.0       1.00      1.00      1.00       643
        1.0       1.00      1.00      1.00       579
        2.0       1.00      1.00      1.00       555
        3.0       1.00      1.00      1.00       620
        4.0       1.00      1.00      1.00       582
        5.0       1.00      0.99      1.00       561
        6.0       1.00      1.00      1.00       574
        7.0       1.00      1.00      1.00       506
        8.0       0.99      1.00      0.99       475
        9.0       0.99      1.00      1.00       511

avg / total       1.00      1.00      1.00      5606


In [13]:
confusion_matrix(y_test, rbf_predict)


Out[13]:
array([[641,   0,   0,   1,   0,   0,   0,   0,   0,   1],
       [  0, 579,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0, 555,   0,   0,   0,   0,   0,   0,   0],
       [  1,   0,   0, 618,   1,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0, 582,   0,   0,   0,   0,   0],
       [  2,   0,   0,   0,   1, 557,   0,   0,   1,   0],
       [  0,   0,   0,   0,   0,   0, 574,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0, 506,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0, 473,   2],
       [  0,   0,   0,   0,   0,   0,   0,   0,   2, 509]])

In [14]:
sum(y_test != rbf_predict)


Out[14]:
12

In [ ]: