Data Science

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

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.

Recent Posts

Mathematics Topics for Machine Learning Beginners

In this blog, you would get to know the essential mathematical topics you need to…

6 days ago

Questions to Ask When Thinking Like a Product Leader

This blog represents a list of questions you can ask when thinking like a product…

1 week ago

Three Approaches to Creating AI Agents: Code Examples

AI agents are autonomous systems combining three core components: a reasoning engine (powered by LLM),…

2 weeks ago

What is Embodied AI? Explained with Examples

Artificial Intelligence (AI) has evolved significantly, from its early days of symbolic reasoning to the…

2 months ago

Retrieval Augmented Generation (RAG) & LLM: Examples

Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…

5 months ago

How to Setup MEAN App with LangChain.js

Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…

5 months ago