Basic SVM in Python

In Python we can build SVM model for classification with sklearn library. We can use basic linearsvc or svc with more parameters to tune.
We use the data from sklearn library, and the IDE is sublime text3. Most of the code comes from the book: https://www.goodreads.com/book/show/32439431-introduction-to-machine-learning-with-python?from_search=true

from sklearn.svm import LinearSVC
from sklearn.svm import SVC
import matplotlib.pyplot as plt 
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer

cancer=load_breast_cancer()
X_train,X_test,y_train,y_test=train_test_split(cancer.data,cancer.target,random_state=46)
svc=SVC()
svc.fit(X_train,y_train)
print(svc.score(X_train, y_train))
print(svc.score(X_test, y_test))
###1.0
###0.629370629371
###with such horrible accuracy, we can plot the data for visulization 

plt.plot(X_train.min(axis=0),'o',label='min')
plt.plot(X_train.max(axis=0),'o',label='max')
plt.legend()
plt.yscale('log')
plt.show()
###we can see the data of cancer is totally assigned with differnet power. Scaling is necessary
###we will manually scale the data
min_on_training=X_train.min(axis=0)
range_on_training=(X_train-min_on_training).max(axis=0)
X_train_scaled=(X_train-min_on_training)/range_on_training
X_test_scaled = (X_test - min_on_training) / range_on_training
svc_scaled=SVC()
svc_scaled.fit(X_train_scaled, y_train)
print("accuracy on training set: %f" % svc_scaled.score(X_train_scaled, y_train))
print("accuracy on test set: %f" % svc_scaled.score(X_test_scaled, y_test))
###accuracy on training set: 0.950704
###accuracy on test set: 0.951049

###rebuild the svm model with parameters. Increase either C or gamma to fit a more complex model:
svc_s_1000= SVC(C=1000)
svc_s_1000.fit(X_train_scaled, y_train)
print("accuracy on training set: %f" % svc_s_1000.score(X_train_scaled, y_train))
print("accuracy on test set: %f" % svc_s_1000.score(X_test_scaled, y_test))
###accuracy on training set: 0.995305
###accuracy on test set: 0.979021

figure_1.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s