In this post, you will learn about how to train an SVM Classifier using Scikit Learn or SKLearn implementation with the help of code examples/samples.
Scikit Learn offers different implementations such as the following to train an SVM classifier.
- LIBSVM: LIBSVM is a C/C++ library specialised for SVM. The SVC class is the LIBSVM implementation and can be used to train the SVM classifier (hard/soft margin classifier).
- Native Python implementation: Scikit Learn provides python implementation of SVM classifier in form SGDClassifier which is based on a stochastic gradient algorithm.
LIBSVM SVC Code Example
In this section, the code below makes use of SVC class (from sklearn.svm import SVC) for fitting a 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))
SVM Python Implementation Code Example
In this section, you will see the usage of SGDClassifier (Note from sklearn.linear_model import SGDClassifier)which is native python implementation. The code below represents the implementation with default parameters.
from sklearn.linear_model import SGDClassifier # Instantiate SVM classifier using SGDClassifier svm = SGDClassifier(loss='hinge') # Fit the model svm.fit(X_train_std, y_train) # Model Performance y_pred = svm.predict(X_test_std) print('Accuracy: %.3f' % accuracy_score(y_test, y_pred))
- Beta Distribution Explained with Python Examples - September 24, 2020
- Bernoulli Distribution Explained with PythonExamples - September 23, 2020
- K-Nearest Neighbors Explained with Python Examples - September 22, 2020