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:
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
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))
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).
When building a regression model or performing regression analysis to predict a target variable, understanding…
If you've built a "Naive" RAG pipeline, you've probably hit a wall. You've indexed your…
If you're starting with large language models, you must have heard of RAG (Retrieval-Augmented Generation).…
If you've spent any time with Python, you've likely heard the term "Pythonic." It refers…
Large language models (LLMs) have fundamentally transformed our digital landscape, powering everything from chatbots and…
As Large Language Models (LLMs) evolve into autonomous agents, understanding agentic workflow design patterns has…