Welcome to ML Using Python (MLUP-101) 18 Oct 2024 Support Vector Machines Demo M08 P02 Session 15. In this tutorial, we will be discussing Support Vector Machines (SVM) using Python.
Support Vector Machines are a powerful and versatile machine learning algorithm that is capable of performing both classification and regression tasks. In SVM, we aim to find the hyperplane that best separates the classes in a high-dimensional space.
In this tutorial, we will be using the popular Python library, scikit-learn, to implement SVM. Scikit-learn is a simple and efficient tool for data mining and data analysis, built on NumPy, SciPy, and matplotlib.
To get started, make sure you have scikit-learn installed. You can install it using pip:
pip install -U scikit-learn
Once you have scikit-learn installed, let’s import the necessary libraries:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
Next, let’s load a dataset to work with. We will be using the famous Iris dataset, which contains information about three different classes of iris flowers.
iris = datasets.load_iris()
X = iris.data[:, :2] # we only take the first two features for visualization purposes
y = iris.target
Now, let’s fit a Support Vector Machine model to the data:
model = svm.SVC(kernel='linear', C=1.0)
model.fit(X, y)
After fitting the model, we can plot the decision boundary:
# plot the decision boundary
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
np.arange(y_min, y_max, 0.02))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.show()
This code snippet will plot the decision boundary of the SVM model along with the data points from the Iris dataset.
Support Vector Machines are a powerful tool in machine learning and can be used for a variety of tasks, including classification and regression. In this tutorial, we have discussed how to implement SVM using Python and scikit-learn.
I hope this tutorial was helpful in understanding Support Vector Machines in Python. Thank you for reading!