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).
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…