Skip to content

Python Program to Implement Support Vector Machine

  • by

Write down the step by step python program to implement support vector machine.

Support Vector Machines (SVM) is very popular machine learning algorithm used for classification and regression problems. SVM is based on the concept of finding hyperplane that best separates data into different classes. Today we will learn how to implement SVM using Python.

Python Program to Handle Missing Values in Data

SVM Algorithm

SVM works by finding optimal hyperplane that separates the data points into different classes. The hyperplane is chosen such that the distance between the hyperplane and the closest data points from each class is maximized. These closest data points are called support vectors.

In SVM we aim to find a decision boundary (hyperplane) that maximizes margin between the closest points of different classes. The decision boundary is defined by the equation:

f(x) = w^T * x + b

where w is the weight vector x is the input vector and b is the bias term.

Python Program to Implement Support Vector Machine

We will use scikit learn library to implement SVM. The scikit learn library provides high level interface for SVM making it easy to implement and tune the model.

The first step is to import necessary libraries

import pandas as pd
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

Next we will load the dataset. For this example we will use the Iris dataset which is a popular dataset for classification problems.

# Load the dataset
iris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
                   names=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class'])

We can use the train_test_split function from scikit-learn to split the dataset into training and testing sets

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.drop('class', axis=1), iris['class'], test_size=0.3, random_state=42)

Next we create an SVM classifier object using the svm.SVC() function. We can specify the kernel function to use for the SVM. In this example we will use the Radial Basis Function (RBF) kernel.

# Create an SVM classifier object
clf = svm.SVC(kernel='rbf')

We then fit the classifier to the training data using the fit() function.

# Fit the SVM classifier to the training data
clf.fit(X_train, y_train)

Finally we use the trained SVM classifier to make predictions on the testing data and calculate the accuracy score.

# Make predictions on the testing data
y_pred = clf.predict(X_test)

# Calculate the accuracy score
accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

Complete Code

import pandas as pd
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the dataset
iris = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data',
                   names=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class'])

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.drop('class', axis=1), iris['class'], test_size=0.3, random_state=42)

# Create an SVM classifier object
clf = svm.SVC(kernel='rbf')

# Fit the SVM classifier to the training data
clf.fit(X_train, y_train)

# Make predictions on the testing data
y_pred = clf.predict(X_test)

# Calculate the accuracy score
accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

In this code we first import the necessary libraries including pandas, scikit-learn’s SVM module, train_test_split and accuracy_score. We then load the Iris dataset from the UCI Machine Learning Repository using the read_csv() function from pandas.

We split the dataset into training and testing sets using the train_test_split() function from scikit-learn. We use 70% of the data for training and the remaining 30% for testing.

Next we create an SVM classifier object using the svm.SVC() function from scikit-learn. We specify the kernel function to be the Radial Basis Function (RBF) kernel.

We fit the SVM classifier to the training data using the fit() function and make predictions on the testing data using the predict() function.

Finally we calculate the accuracy score of the SVM classifier on the testing data using the accuracy_score() function from scikit-learn, and print the result.

Python Useful Links:

Leave a Reply

Your email address will not be published. Required fields are marked *