SVM Implementation
SVM Implementation
Now we will implement the SVM algorithm using Python. Here we will use
the same dataset user_data, which we have used in Logistic regression
and KNN classification.
Till the Data pre-processing step, the code will remain the same. Below is
the code:
After executing the above code, we will pre-process the data. The code will
give the dataset as:
The scaled output for the test set will be:
Fitting the SVM classifier to the training set:
Now the training set will be fitted to the SVM classifier. To create the SVM
classifier, we will import SVC class from Sklearn.svm library. Below is the
code for it:
Output:
Out[8]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto_deprecated',
kernel='linear', max_iter=-1, probability=False, random_state=0,
shrinking=True, tol=0.001, verbose=False)
Output: Below is the output for the prediction of the test set:
o Creating the confusion matrix:
Now we will see the performance of the SVM classifier that how many
incorrect predictions are there as compared to the Logistic regression
classifier. To create the confusion matrix, we need to import
the confusion_matrix function of the sklearn library. After importing
the function, we will call it using a new variable cm. The function
takes two parameters, mainly y_true( the actual values)
and y_pred (the targeted value return by the classifier). Below is the
code for it:
Output:
As we can see in the above output image, there are 66+24= 90 correct
predictions and 8+2= 10 correct predictions. Therefore we can say that our
SVM model improved as compared to the Logistic regression model.
Output:
Output:
As we can see in the above output image, the SVM classifier has divided
the users into two regions (Purchased or Not purchased). Users who
purchased the SUV are in the red region with the red scatter points. And
users who did not purchase the SUV are in the green region with green
scatter points. The hyperplane has divided the two classes into Purchased
and not purchased variable.