SVM Classification

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Welcome to another machine learning tutorial. We learned about K Nearest neighbors in previous article. Now we will learn SVM classification and see different types of SVM kernels.

SVM Classification:

Support Vector Machines are popular in machine learning. In this article, we will perform SVM classification with different types of kernels provided by sklearn.svm library’s SVC class. We will use the same problem statement used in logistic regression article. We want to know whether a particular user will buy our car or not.

Data Preprocessing:

  • We read “Social_Network_Ads.csv” file and stored in dataset.
  • Extracted age and salary information from dataset and stored in X
  • Extracted purchase information from dataset and stored in Y.
  • Split dataset in training and test set so that machine can be trained using X_train and Y_train and X_test and y_test can be used as test set.
  • Used feature scaling for X_train.

Linear SVM Classification:

  • We imported Support Vector Classifier  i.e. SVC from sklearn.svm
  • created a classifier object with a linear kernel
  • fitted classifier with X_train and y_train.

That’s it, we have created our classifier, let plot classification graph and compare actual i.e. y_test results.

  • aranged_ages variable will have scaled ages of users starting from minimum age to maximum age incremented by 0.01.
  • aranged_salaries variable will have scaled salaries of users starting from minimum salary to maximum salary incremented by 0.01.
  • np.meshgrid() takes aranged_ages and aranged_salaries to form X1 and X2.
  • X1 and X2 are used for creating a graph which classifies all data points using SVM classification. It is done using plt.contourf(), method.
  • Then we plot actual data points from test set on graph to compare them with classification.
SVM Linear Kernel
SVM Linear Kernel
  • Orange and blue sections denote users who will not and who will buy the car respectively
  • red and green points denote users who didn’t buy and who bought the car.
  • As per graph, we can see 8 green points in orange area and 2 red points in blue area. It means out of 100, 10 predictions are wrong and 90 are correct

RBF SVM Classification:

Now, simply create classifer as below

and change title of plot as below

Execute complete program to see results of RBF classifier.

SVM RBF Kernel
SVM RBF Kernel

Note 4 red points in blue area and 3 green points in orange area. It means 93 predictions out of 100 are correct and 7 are wrong

Polynomial SVM Classification:

Create a polynomial classifier as below

and change plot title as below

Execute the program and graph is plotted as follows

SVM Polynomial Kernel
SVM Polynomial Kernel

There are 13 green points in orange area and 1 red point in blue area. It means out of 100, 86 predictions are right and 14 are wrong.

Sigmoid SVM Classification:

Create a polynomial classifier as below

and change plot title as below

Execute the program and graph is plotted as follows

SVM Sigmoid Kernel
SVM Sigmoid Kernel

12 green points are observed in orange area and 14 red points in blue area. It means out of 100, only 74 predictions are right and 26 are wrong.

I hope this article gave brief introduction to different types of SVM classification kernels. Happy learning 🙂

 

References:

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *