Supervised Learning — Support Vector Machine
Linear classifier has one nine or one plane that separate the two groups. Whereas Support Vector Machine(SVM) try to find two parallel lines or plans with the widest margin to separate the two groups. In the figure above rightmost is the correct example of the a dataset classified by a SVM.
In case when we have non-linear dataset as depicted above, SVM usage SVM Kernels for data transformation. Most popular kernels are linear, nonlinear, polynomial, radial basis function (RBF), and sigmoid.
Create Train and Test Data sets
#Import scikit-learn dataset library
from sklearn import datasets#Load dataset
cancer = datasets.load_breast_cancer()# Import train_test_split function
from sklearn.model_selection import train_test_split# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, test_size=0.3,random_state=109) # 70% training and 30% testCreate SVM Model
#Import svm model
from sklearn import svm#Create a svm Classifier
clf = svm.SVC(kernel=’linear’) # Linear KernelTrain the Model
#Train the model using the training sets
clf.fit(X_train, y_train)#Predict the response for test dataset
y_pred = clf.predict(X_test)Calculate Accuracy
#Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics# Model Accuracy: how often is the classifier correct?
print(“Accuracy:”,metrics.accuracy_score(y_test, y_pred))