Sklearn SVM Classifier using LibSVM – Code Example

In this post, you learn about Sklearn LibSVM implementation used for training an SVM classifier, with code example.  Here is a great guide for learning SVM classification, especially, for beginners in the field of data science/machine learning.

LIBSVM is a library for Support Vector Machines (SVM) which provides an implementation for the following:

  • C-SVC (Support Vector Classification)
  • nu-SVC
  • epsilon-SVR (Support Vector Regression)
  • nu-SVR
  • Distribution estimation (one-class SVM)

In this post, you will see code examples in relation to C-SVC, and nu-SVC LIBSVM implementations. I will follow up with code examples for SVR and distribution estimation in future posts. Here are the links to their SKLearn pages for C-SVC and nu-SVC

Sklearn LibSVM (C-SVC) Code Example

In this section, you will see the code example for training an SVM classifier based on C-SVC implementation within LibSVM. Note that C is a regularization parameter that is used to train a soft-margin classifier allowing for bias-variance tradeoff based on the value of C. A detailed post on C value can be found in this post, SVM as soft margin classifier and C value. Here is the code. Note the instantiation of SVC class in this statement, svm = SVC(kernel= ‘linear’, random_state=1, C=0.1). Iris data set is used for training the model.

import pandas as pd
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn import datasets

# IRIS Data Set

iris = datasets.load_iris()
X = iris.data
y = iris.target

# Creating 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)

# Feature Scaling

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

# Training a SVM classifier using SVC class
svm = SVC(kernel= 'linear', random_state=1, C=0.1)
svm.fit(X_train_std, y_train)

# Mode performance

y_pred = svm.predict(X_test_std)
print('Accuracy: %.3f' % accuracy_score(y_test, y_pred))

Sklearn LibSVM (Nu-SVC) Code Example

In this section, you will see a code sample on how to train a SVM classifier using nuSVC implementation.

from sklearn.svm import NuSVC

# Instantiate the nuSVC implementation
nusvc = NuSVC(nu=0.03)

# Fit the model
nusvc.fit(X_train_std, y_train)

# Mode performance
y_pred = nusvc.predict(X_test_std)
print('Accuracy: %.3f' % accuracy_score(y_test, y_pred))

One could find further details on SVM LIBSVM implementation on this page for building a classifier (classification model).

Ajitesh Kumar

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. 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 , , .