Wednesday 2 May 2018

Simple implementation of SVM in python

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
from sklearn import svm



#train set
x = [[86,105], [109, 100], [94, 105], [106, 100], [100, 100], 
     [80, 90], [103, 80], [105, 80], [120, 85], [77, 83], [92, 75], 
     [98, 76], [106, 82],[106, 77], [105, 77], [119, 80], [115, 70], 
     [110, 66], [105, 65], [90, 67], [80, 60], [90, 57], [105, 55],
     [115, 55], [110, 50], [109, 49], [95, 45], [100, 42], [105, 40],
     [110, 42], [115, 42], [115,35], [105, 35], [85, 35], [95, 35], [109, 35],
     [115, 35], [120, 30], [105, 29], [109, 25]]

#train set lebels
y = [130, 130, 130, 130, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120,
     120, 110, 110, 110, 110, 100, 100, 100, 100, 90, 90, 80, 80, 80, 80, 80, 70, 
     70, 60, 60, 60, 60, 50, 50, 40]
clf = svm.SVC(gamma=0.001, C=100)

clf.fit(x,y)

from sklearn.externals import joblib
joblib.dump(clf, 'C:/Users/kiit1/Desktop/svm.pkl')
test_set = [[86,105], [109, 100], [80, 90], [103, 80], [98, 76], [106, 82],
 [105, 77], [119, 80], [90, 57], [105, 55], [119, 27]]
y_test = [130, 130, 120, 123, 120, 120, 120, 120, 100, 100, 40]
# model accuracy for X_testaccuracy = clf.score(test_set, y_test)
print(clf.predict(test_set))
print(accuracy)

# creating a confusion matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, clf.predict(test_set))
print(cm)

clf1 = joblib.load('C:/Users/kiit1/Desktop/svm.pkl')
print(clf1.predict([[86,105]]))

Output