SVM
(Support Vector Machine)
SVM
(Support Vector Machine)
INTRODUCTION:
Support Vector Machine (SVM) is a supervised machine learning algorithm that is mainly used for:
- Classification
- Regression
- Outlier detection
SVM works by finding the best possible boundary (called a hyperplane) that separates different classes in the dataset. The goal is to maximize the margin, which is the distance between the nearest data points of each class and the decision boundary.
In this project, we use SVM to classify flowers from the Iris dataset based on their measurements.
2. Why SVM?
SVM is powerful because:
- It works well for small and medium datasets
- It is effective in high-dimensional spaces
- It finds the optimal separating boundary
- It avoids overfitting by maximizing margin
3. Dataset Used — Iris Dataset
The Iris dataset contains:
- 150 flower samples
- 4 features:
- Sepal length
- Sepal width
- Petal length
- Petal width
- 3 classes:
- Setosa
- Versicolor
- Virginica

CODE:
Code Implementation and Line-by-Line Explanation
from sklearn.svm import SVC
This line imports the Support Vector Classifier from the sklearn library. SVC is the main class used to create an SVM model for classification tasks.
from sklearn.model_selection import train_test_split
This line imports the function that divides the dataset into training data and testing data. This is important so that we can train the model on one part of the data and test it on unseen data.
from sklearn.datasets import load_iris
This line loads the built-in Iris dataset from sklearn. It gives us both the feature values and their corresponding flower labels.
import matplotlib.pyplot as plt
This line imports Matplotlib, which is used to draw graphs and visualize results.
import seaborn as sns
This line imports Seaborn, which helps in creating attractive statistical plots such as heatmaps.
from sklearn.metrics import confusion_matrix, accuracy_score
This line imports two evaluation metrics. The confusion matrix shows how many predictions are correct and wrong for each class, and accuracy_score tells how accurate the model is.
X = load_iris().data
This line extracts all the feature values from the Iris dataset. These values include sepal length, sepal width, petal length, and petal width.
y = load_iris().target
This line extracts the target labels from the dataset. These are the flower types that we want the model to predict.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
This line splits the dataset into training and testing sets. 80% of the data is used for training and 20% for testing. The random_state is set so that the split remains the same every time the code runs.
model = SVC()
This line creates an SVM model. It initializes the Support Vector Classifier.
model.fit(X_train, y_train)
This line trains the SVM model using the training data. The model learns the pattern that separates different flower types.

y_pred = model.predict(X_test)
This line uses the trained model to predict the flower classes for the testing data.
acc = accuracy_score(y_test, y_pred)
This line calculates how many predictions are correct out of the total predictions.
cm = confusion_matrix(y_test, y_pred)
This line creates a confusion matrix that shows how many flowers were correctly and incorrectly classified.
sns.heatmap(cm, annot=True)
This line draws a heatmap of the confusion matrix. The numbers inside the boxes show prediction counts.
plt.xlabel("Predicted")
This labels the x-axis of the heatmap as predicted values.
plt.ylabel("Actual")
This labels the y-axis of the heatmap as actual values.
plt.show()
This displays the final heatmap on the screen.

Functions Used in SVM Program — Complete Explanation
1. load_iris()
This function loads the Iris dataset that is already available inside the sklearn library.
It returns a dataset that contains:
- Flower measurements (features)
- Flower categories (labels)
- Feature names
- Target names
We use load_iris() so that we don’t need to manually download or create a dataset. It gives us a clean, ready-to-use dataset for machine learning experiments.
When we write:
load_iris().data
it gives all the input values (sepal length, sepal width, petal length, petal width).
When we write:
load_iris().target
it gives the output labels (0, 1, or 2 representing flower types).
2. train_test_split()
This function is used to divide the dataset into two parts
- Training data
- Testing data
Machine learning models should not be tested on the same data they are trained on. So this function ensures the model is evaluated on unseen data.
We use it like this:
train_test_split(X, y, test_size=0.2, random_state=42)
test_size=0.2 means 20% of the data is kept for testing.
random_state=42 makes sure that the same rows go into training and testing every time the program runs, which makes the results reproducible.
3. SVC()
This function creates the Support Vector Machine model.
SVC stands for Support Vector Classifier. It uses the SVM algorithm to find the best boundary that separates different classes.
When we write:
model = SVC()
we are creating a machine learning model that is ready to learn from data.
4. fit()
The fit() function is used to train the model.
It takes training data as input and allows the model to learn patterns.
model.fit(X_train, y_train)
Here, SVM analyzes all the feature values and their correct labels and finds the best hyperplane that separates the flower classes.
Without fit(), the model cannot learn anything.
5. predict()
This function is used to make predictions on new or unseen data.
y_pred = model.predict(X_test)
Here, the model uses what it learned during training and predicts the flower type for each test sample.
This is how we check whether the model learned correctly.
6. accuracy_score()
This function calculates how accurate the model is.
accuracy_score(y_test, y_pred)
It compares the actual flower types (y_test) with the predicted flower types (y_pred) and returns a value between 0 and 1.
If accuracy is 0.95, it means 95% of predictions are correct.
7. confusion_matrix()
This function shows detailed performance of the model.
confusion_matrix(y_test, y_pred)
It creates a matrix that tells:
- How many samples were correctly classified
How many were wrongly classified into another class
It is more informative than accuracy because it shows mistakes for each class.
8. sns.heatmap()
This function draws the confusion matrix in graphical form.
sns.heatmap(cm, annot=True)
It converts the numeric matrix into a color-coded grid, making it easy to understand model performance visually.
annot=True means the numbers are shown inside each box.
9. plt.xlabel() and plt.ylabel()
These functions label the axes of the graph.
They make the graph understandable by telling which side shows predicted values and which side shows actual values.
10. plt.show()
This function displays the final graph on the screen.
Without this line, the heatmap would not appear.
How SVM Works:
SVM tries to find the best line or plane that divides the data into different classes. It does not just find any line but chooses the one that gives the largest margin between the classes. The data points that are closest to this boundary are called support vectors. These points control the position of the hyperplane and make the model strong and accurate.
Conclusion:
Support Vector Machine is a very powerful machine learning algorithm for classification tasks. In this project, it successfully classified Iris flowers using their measurements. Because SVM focuses on maximizing the margin between classes, it produces highly accurate and reliable results. This makes SVM suitable for real-world applications such as image recognition, medical diagnosis, and text classification.
THANK YOU!
by:
PRIYAM TIWARI
AIML
RUNGTA COLLEGE OF ENGINERRING AND TECHNOLOGY
메타데이터
- post_id
- c3d2d4da12d9
- slug
- svm-c3d2d4da12d9
- url
- https://medium.com/@priyamtiwari668/svm-c3d2d4da12d9
- canonical_url
- https://medium.com/@priyamtiwari668/svm-c3d2d4da12d9
- author_url
- https://medium.com/@priyamtiwari668
- status
- ok
- fetched_at
- 2026-07-09 10:05:04