Support Vector Machine (SVM Classifier) Implemenation in Python With Scikit-Learn
Support Vector Machine (SVM Classifier) Implemenation in Python With Scikit-Learn
Given fruit features like color, size, taste, weight, shape. Predicting the fruit
type.
By analyzing the skin, predicting the different skin disease.
Given Google news articles, predicting the topic of the article. This could be
sport, movie, tech news related article, etc.
In the first example of predicting the fruit type. The target class will have many
fruits like apple, mango, orange, banana, etc. This is same with the other two
examples in predicting. The problem of the new article, the target class having
different topics like sport, movie, tech news ..etc
In this article, we were going to implement the svm classifier with different kernels.
However, we have explained the key aspect of support vector machine algorithm as
well we had implemented svm classifier in R programming language in our earlier
posts. If you are reading this post for the first time, it’s recommended to chek out
the previous post on svm concepts.
To implement svm classifier in Python, we are going to use the one of most popular
classification dataset which is Iris dataset. Let’s quickly look at the features and the
target variable details of the famous classification dataset.
This famous classification dataset first time used in Fisher’s classic 1936 paper, The
Use of Multiple Measurements in Taxonomic Problems. Iris dataset is having 4
features of iris flower and one target class.
SepalLengthCm
SepalWidthCm
PetalLengthCm
PetalWidthCm
The flower species type is the target class and it having 3 types
Setosa
Versicolor
Virginica
The idea of implementing svm classifier in Python is to use the iris features to train
an svm classifier and use the trained svm model to predict the Iris species type. To
begin with let’s try to load the Iris dataset. We are going to use the iris data
from Scikit-Learn package.
1 # Required Packages
2 from sklearn import datasets # To Get iris dataset
3 from sklearn import svm # To fit the svm classifier
4 import numpy as np
5 import matplotlib.pyplot as plt # To visuvalizing the data
Using the DESCR key over the iris_dataset, we can get description of the dataset
Output
1 Iris data set Description :: Iris Plants Database
2
3 Notes
4 -----
5 Data Set Characteristics:
6 :Number of Instances: 150 (50 in each of three classes)
7 :Number of Attributes: 4 numeric, predictive attributes and the class
8 :Attribute Information:
9 - sepal length in cm
10 - sepal width in cm
11 - petal length in cm
12 - petal width in cm
13 - class:
14 - Iris-Setosa
15 - Iris-Versicolour
16 - Iris-Virginica
17 :Summary Statistics:
18
19 ============== ==== ==== ======= ===== ====================
20 Min Max Mean SD Class Correlation
21 ============== ==== ==== ======= ===== ====================
22 sepal length: 4.3 7.9 5.84 0.83 0.7826
23 sepal width: 2.0 4.4 3.05 0.43 -0.4194
24 petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
25 petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
26 ============== ==== ==== ======= ===== ====================
27
28 :Missing Attribute Values: None
29 :Class Distribution: 33.3% for each of 3 classes.
30 :Creator: R.A. Fisher
31 :Donor: Michael Marshall (MARSHALL%[email protected])
32 :Date: July, 1988
33
34 This is a copy of UCI ML iris datasets.
35 http://archive.ics.uci.edu/ml/datasets/Iris
36
37 The famous Iris database, first used by Sir R.A Fisher
38
39 This is perhaps the best known database to be found in the
40 pattern recognition literature. Fisher's paper is a classic in the field and
41 is referenced frequently to this day. (See Duda & Hart, for example.) The
42 data set contains 3 classes of 50 instances each, where each class refers to a
43 type of iris plant. One class is linearly separable from the other 2; the
44 latter are NOT linearly separable from each other.
45
46 References
47 ----------
48 - Fisher,R.A. "The use of multiple measurements in taxonomic problems"
49 Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
50 Mathematical Statistics" (John Wiley, NY, 1950).
51 - Duda,R.O., & Hart,P.E. (1973) Pattern Classification and Scene Analysis.
52 (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.
53 - Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
54 Structure and Classification Rule for Recognition in Partially Exposed
55 Environments". IEEE Transactions on Pattern Analysis and Machine
56 Intelligence, Vol. PAMI-2, No. 1, 67-71.
57 - Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions
58 on Information Theory, May 1972, 431-433.
59 - See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II
60 conceptual clustering system finds 3 classes in the data.
61 - Many, many more ...
Now let’s get the iris features and the target classes
Output
As we are said, these are 4 features first 2 were sepal length, sepal width and the
next 2 were petal length and width. Now let’s check the target data
1 print "Iris target :: ", iris_dataset['target']
Output
1 Iris target :: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 0000000000000111111111111111111111111
3 1111111111111111111111111122222222222
4 2222222222222222222222222222222222222
5 2 2]
Let’s take the individual features like sepal, petal length, and weight and let’s
visualize the corresponding target classes with different colors.
1 def visuvalize_sepal_data():
2 iris = datasets.load_iris()
3 X = iris.data[:, :2] # we only take the first two features.
4 y = iris.target
5 plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
6 plt.xlabel('Sepal length')
7 plt.ylabel('Sepal width')
8 plt.title('Sepal Width & Length')
9 plt.show()
10
11 visuvalize_sepal_data()
To visualize the Sepal length, width and corresponding target classes we can create
a function with name visuvalize_sepal_data. At the beginning, we are loading the
iris dataset to iris variable. Next, we are storing the first 2 features in iris dataset
which are sepal length and sepal width to variable x. Then we are storing the
corresponding target values in variable y.
As we have seen target variable contains values like 0, 1,2 each value represents
the iris flower species type. Then we are plotting the points on XY axis on X-axis we
are plotting Sepal Length values. On Y-axis we are plotting Sepal Width values. If
you follow installing instruction correctly on Installing Python machine learning
packages and run the above code, you will get the below image.
Let’s create the similar kind of graph for Petal length and width
Visualizing the relationship between Petal and target classes
1 def visuvalize_petal_data():
2 iris = datasets.load_iris()
3 X = iris.data[:, 2:] # we only take the last two features.
4 y = iris.target
5 plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.coolwarm)
6 plt.xlabel('Petal length')
7 plt.ylabel('Petal width')
8 plt.title('Petal Width & Length')
9 plt.show()
10
11 visuvalize_petal_data()
As we have successfully visualized the behavior of target class (iris species type)
with respect to Sepal length and width as well as with respect to Petal length and
width. Now let’s model different kernel Svm classifier by considering only the Sepal
features (Length and Width) and only the Petal features (Lenght and Width)
1 iris = datasets.load_iris()
2 X = iris.data[:, :2] # we only take the Sepal two features.
3 y = iris.target
4 C = 1.0 # SVM regularization parameter
5
6 # SVC with linear kernel
7 svc = svm.SVC(kernel='linear', C=C).fit(X, y)
8 # LinearSVC (linear kernel)
9 lin_svc = svm.LinearSVC(C=C).fit(X, y)
10 # SVC with RBF kernel
11 rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y)
12 # SVC with polynomial (degree 3) kernel
13 poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y)
To model different kernel svm classifier using the iris Sepal features, first, we
loaded the iris dataset into irisvariable like as we have done before. Next, we are
loading the sepal length and width values into X variable, and the target values
are stored in y variable. Once we are ready with data to model the svm classifier,
we are just calling the scikit-learn svm module function with different kernels.
Now let’s visualize the each kernel svm classifier to understand how well the
classifier fit the train features.
If we run the above code, we will get the below graph. From which we can
understand how well different kernel svm classifiers are modeled.
1 iris = datasets.load_iris()
2 X = iris.data[:, 2:] # we only take the last two features.
3 y = iris.target
4 C = 1.0 # SVM regularization parameter
5
6 # SVC with linear kernel
7 svc = svm.SVC(kernel='linear', C=C).fit(X, y)
8 # LinearSVC (linear kernel)
9 lin_svc = svm.LinearSVC(C=C).fit(X, y)
10 # SVC with RBF kernel
11 rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y)
12 # SVC with polynomial (degree 3) kernel
13 poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y)
The above code is much similar to the previously modeled svm classifiers code. The
only difference is loading the Petal features into X variable. The remaining code is
just the copy past from the previously modeled svm classifier code.
Now let’s visualize the each kernel svm classifier to understand how well the
classifier fit the Petal features.
If we run the above code, we will get the below graph. From which we can
understand how well different kernel svm classifiers are modeled.
Svm Classifier with Iris Petal features
This is how the modeled svm classifier looks like when we only use the petal width
and length to model. With this, we came to an end. Before put an end to the post
lets quickly look how to use the modeled svm classifier to predict iris flow
categories.
To Identify the iris flow type using the modeled svm classifier, we need to call the
predict function over the fitted model. For example, if you want to predict the iris
flower category using the lin_svc model. We need to call lin_svc.predict(with the
features). In our case, these features will include the sepal length and width or
petal length and width. If you are not clear with the using the predict function
correctly you check knn classifier with scikit-learn.
Conclusion
In this article, we learned how to model the support vector machine classifier using
different, kernel with Python scikit-learn package. In the process, we have learned
how to visualize the data points and how to visualize the modeled svm classifier for
understanding the how well the fitted modeled were fit with the training dataset.
Related Articles
Related
classification algorithms
Svm
Previous Post
Support Vector Machine Classifier Implementation in R with caret package
Data Science
Next Post
How Decision Tree Algorithm works
Data Science
Search
FOLLOW AUTHOR