Implementing SVM and Kernel SVM with Python's Scikit-Learn
Last Updated :
23 Jul, 2025
In this article we will implement a classification model using Scikit learn implementation for SVM model in Python. Then we will try to understand what is a kernel and how it can helps us to achieve better performance by learning non-linear boundaries in the dataset.
What is a SVM algorithm?
Support vector machines (SVMs) are a type of supervised learning algorithm that can be used for classification or regression tasks. In simple terms, an SVM constructs a hyperplane or set of hyperplanes in a high-dimensional space, which can be used to separate different classes or to predict continuous variables.
The main idea behind SVMs is to find the hyperplane in the high-dimensional space that has the maximum margin, or the maximum distance between the data points of the different classes. This hyperplane is called the maximum-margin hyperplane, and the data points that are closest to the hyperplane are called support vectors. The position and orientation of the maximum-margin hyperplane can be determined using mathematical optimization techniques.
SVMs have been widely used in many applications, including image and text classification, protein classification, and regression problems. They have several advantages, such as the ability to handle high-dimensional data, the ability to perform non-linear classification using the kernel trick, and the ability to provide probability estimates for classification tasks. However, they can be computationally expensive and may not be suitable for very large datasets.
To show the usage of the kernel SVM let's import the necessary libraries and the iris dataset.
Python3
# Import necessary libraries
from sklearn import svm
from sklearn import datasets
# Load the Iris dataset
iris = datasets.load_iris()
# We only take the first two
# features for simplicity
X = iris.data[:, :2]
y = iris.target
Now we will use SupportVectorClassifier as currently we are dealing with a classification problem.
Python3
# Fit the SVM model
model = svm.SVC(kernel='linear')
model.fit(X, y)
# Predict using the SVM model
predictions = model.predict(X)
# Evaluate the predictions
accuracy = model.score(X, y)
print("Accuracy of SVM:", accuracy)
Output :
Accuracy of SVM: 0.82
The above code is an example of using a support vector machine (SVM) model to make predictions on the Iris dataset. The Iris dataset is a well-known dataset in machine learning that contains measurements of various characteristics of iris flowers, such as sepal length and width, and the species of the flower.
The code first imports the necessary modules and libraries, including the SVM module from Scikit-learn and the Iris dataset from Scikit-learn's datasets module. Then, it loads the Iris dataset and extracts the first two features from each example (sepal length and width), as well as the target labels (the species of the flower).
Next, the code creates an SVM model using the SVC class from Scikit-learn, and specifies that it should use a linear kernel. Then, it trains the model on the data using the fit() method, and makes predictions on the same data using the predict() method. Finally, it evaluates the predictions by computing the accuracy of the model (the fraction of examples that were predicted correctly) using the score() method. The accuracy is then printed to the console.
What is Kernel SVM?
Kernel support vector machines (SVMs) are a variant of support vector machines (SVMs) that use kernel functions to find the maximum-margin hyperplane in non-linear classification or regression problems. In simple terms, a kernel function transforms the original data into a higher-dimensional space, where it becomes linearly separable. The maximum-margin hyperplane is then found in this higher-dimensional space using an SVM.
Kernel SVMs have several advantages over regular SVMs. They can handle non-linear classification or regression tasks without having to explicitly perform the data transformation, which can be computationally expensive. They also allow the use of different kernel functions, which can provide different types of non-linear transformations and can be chosen based on the specific characteristics of the data.
The most commonly used kernel functions in kernel SVMs are the linear, polynomial, and radial basis function (RBF) kernels. The linear kernel is used for linear classification or regression tasks, the polynomial kernel can handle non-linear problems, and the RBF kernel is often used in classification tasks with a large number of features.
Kernel SVMs have been widely used in many applications, such as image and text classification, protein classification, and regression problems. However, they can be computationally expensive and may not be suitable for very large datasets. Additionally, choosing the right kernel function and the corresponding hyperparameters can be challenging and requires some domain knowledge and experimentation.
Here is an example of how to implement Support Vector Machines (SVM) and Kernel SVM with Python's Scikit-learn library:
Python3
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
# Dummy function to simulate loading the data from a file
def load_data():
X_train = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
X_test = [[10, 11, 12], [13, 14, 15], [16, 17, 18]]
y_train = [1, 0, 1]
y_test = [0, 1, 0]
return X_train, X_test, y_train, y_test
# Load the data and split it into training and test sets
X_train, X_test,\
y_train, y_test = load_data()
Now let's normalize the dataset using the StandardScaler function from the sklearn library this will helps us achieve stable and faster training process of the model.
Python3
# Import the necessary modules and libraries
# Scale the features using standardization
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
Next, we create a KSVM model using the radial basis function (RBF) kernel, with the gamma and C hyperparameters set to 0.1 and 10.0, respectively.
Python3
# Create a kernel support vector machine model
ksvm = svm.SVC(kernel='rbf',
gamma=0.1,
C=10.0)
# Train the model on the training data
ksvm.fit(X_train, y_train)
# Evaluate the model on the test data
accuracy = ksvm.score(X_test, y_test)
print('Accuracy:', accuracy)
Output :
Accuracy: 0.3333333333333333
Difference between the SVM and kernel SVM:
Support vector machines (SVMs)
| kernel support vector machines (KSVMs)
|
Support vector machines (SVMs) is supervised learning algorithm that can be used for classification and regression. | kernel support vector machines (KSVMs) are also supervised learning algorithms that can be used for classification and regression. |
SVMs are more sensitive to the choice of hyperparameters (such as the regularization parameter and kernel function). | KSVMs are less sensitive and can often be trained using default settings. |
SVMs can only handle small to medium-sized datasets. | KSVMs can handle larger datasets due to their efficient training algorithms. |
SVMs are less commonly used in practice than KSVMs. | KSVMs have been shown to perform better on a wider range of tasks and datasets. |
Similar Reads
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.Do you
5 min read
Introduction to Machine Learning
Python for Machine Learning
Machine Learning with Python TutorialPython language is widely used in Machine Learning because it provides libraries like NumPy, Pandas, Scikit-learn, TensorFlow, and Keras. These libraries offer tools and functions essential for data manipulation, analysis, and building machine learning models. It is well-known for its readability an
5 min read
Pandas TutorialPandas is an open-source software library designed for data manipulation and analysis. It provides data structures like series and DataFrames to easily clean, transform and analyze large datasets and integrates with other Python libraries, such as NumPy and Matplotlib. It offers functions for data t
6 min read
NumPy Tutorial - Python LibraryNumPy (short for Numerical Python ) is one of the most fundamental libraries in Python for scientific computing. It provides support for large, multi-dimensional arrays and matrices along with a collection of mathematical functions to operate on arrays.At its core it introduces the ndarray (n-dimens
3 min read
Scikit Learn TutorialScikit-learn (also known as sklearn) is a widely-used open-source Python library for machine learning. It builds on other scientific libraries like NumPy, SciPy and Matplotlib to provide efficient tools for predictive data analysis and data mining.It offers a consistent and simple interface for a ra
3 min read
ML | Data Preprocessing in PythonData preprocessing is a important step in the data science transforming raw data into a clean structured format for analysis. It involves tasks like handling missing values, normalizing data and encoding variables. Mastering preprocessing in Python ensures reliable insights for accurate predictions
6 min read
EDA - Exploratory Data Analysis in PythonExploratory Data Analysis (EDA) is a important step in data analysis which focuses on understanding patterns, trends and relationships through statistical tools and visualizations. Python offers various libraries like pandas, numPy, matplotlib, seaborn and plotly which enables effective exploration
6 min read
Feature Engineering
Supervised Learning
Unsupervised Learning
Model Evaluation and Tuning
Advance Machine Learning Technique
Machine Learning Practice