Classification Model with SVM Classifier – Python Example

In this post, you will get an access to Python code example for building a machine learning classification model using SVM (Support Vector Machine) classifier algorithm. We will work with Python Sklearn package for building the model.

The following steps will be covered for training the model using SVM:

  • Load the data
  • Create training and test split
  • Perform feature scaling
  • Instantiate an SVC classifier
  • Fit the model
  • Measure the model performance

First and foremost we will load appropriate Sklearn modules and classes.

# Basic packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Sklearn modules & classes
from sklearn.linear_model import Perceptron, LogisticRegression
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import datasets
from sklearn import metrics

Lets get started with loading the data set and creating the training and test split from the data set. Pay attention to the stratification aspect used when creating the training and test split. The train_test_split class of sklearn.model_selection is used for creating training and test split.

# Load the data set; In this example, the breast cancer dataset is loaded. 
bc = datasets.load_breast_cancer()
X =
y =

# Create training and test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1, stratify=y)

Next step is to perform feature scaling. The reason for doing feature scaling is to make sure that data for different features are in the same range. The StandardScaler class of sklearn.preprocessing is used.

sc = StandardScaler()
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)

Next step is to instantiate a SVC (Support Vector Classifier) and fit the model. The SVC class of sklearn.svm module is used.

# Instantiate the Support Vector Classifier (SVC)
svc = SVC(C=1.0, random_state=1, kernel='linear')

# Fit the model, y_train)

Finally, it is time to measure the model performance. Here is the code for doing the same:

# Make the predictions
y_predict = svc.predict(X_test_std)

# Measure the performance
print("Accuracy score %.3f" %metrics.accuracy_score(y_test, y_predict))
The performance of the model will turn out to be 0.953. 
Ajitesh Kumar
Follow me

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking
Posted in AI, Data Science, Machine Learning, Python. Tagged with , , .

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload the CAPTCHA.