Support Vector Machine (SVM)
🟦 Program Aim
Aim:
To implement the Support Vector Machine (SVM) algorithm using Python and classify objects into different categories.
🟩 Algorithm Used
Support Vector Machine (SVM) Classifier
🟨 Problem Statement
A fruit shop wants to classify fruits into two categories:
- 🍎 Small Fruit
- 🍉 Large Fruit
The classification is based on the weight of the fruit.
🟪 Step 1: Import the Required Library
First, import the SVC (Support Vector Classifier) class from the sklearn.svm module.
from sklearn.svm import SVC
Explanation
-
sklearnis the Scikit-learn machine learning library. -
svmis the module that contains Support Vector Machine algorithms. -
SVC()is used for classification problems.
🟦 Step 2: Create the Training Dataset
X = [
[2],
[3],
[4],
[5]
]
Explanation
X represents the input feature (Independent Variable).
Here, each value represents the weight of a fruit (in kg).
| Fruit | Weight (kg) |
|---|---|
| Fruit 1 | 2 |
| Fruit 2 | 3 |
| Fruit 3 | 4 |
| Fruit 4 | 5 |
The SVM algorithm learns from these weight values.
🟩 Step 3: Create the Output Labels
y = [
"Small",
"Small",
"Large",
"Large"
]
Explanation
y represents the target labels (Dependent Variable).
| Weight | Category |
|---|---|
| 2 | Small |
| 3 | Small |
| 4 | Large |
| 5 | Large |
The model learns which weight belongs to which category.
🟨 Step 4: Create the SVM Model
model = SVC(kernel="linear")
Explanation
-
SVC()creates the Support Vector Machine model. -
kernel="linear"tells the model to use a Linear Kernel. - The model will find the best straight-line boundary (hyperplane) between the two categories.
🟪 Step 5: Train the Model
model.fit(X, y)
Explanation
The fit() function trains the SVM model.
Syntax
model.fit(X, y)
Where:
-
X= Input data -
y= Output labels
During training, the algorithm:
- Reads the training data.
- Finds the support vectors.
- Calculates the maximum margin.
- Draws the optimal hyperplane.
🟦 Step 6: Predict New Data
Suppose a new fruit has a weight of 4 kg.
prediction = model.predict([[4]])
Explanation
predict() is used to classify new data.
Syntax
model.predict([[value]])
Here,
[[4]]
means the weight of the new fruit is 4 kg.
The model predicts whether it is Small or Large.
🟩 Step 7: Display the Result
print("Prediction =", prediction[0])
Explanation
prediction is returned as a list.
Example:
['Large']
To print only the predicted class, use:
prediction[0]
Output
Prediction = Large
🟨 Complete Python Program
# Import Support Vector Machine
from sklearn.svm import SVC
# Training Data (Fruit Weight)
X = [
[2],
[3],
[4],
[5]
]
# Output Labels
y = [
"Small",
"Small",
"Large",
"Large"
]
# Create SVM Model
model = SVC(kernel="linear")
# Train the Model
model.fit(X, y)
# Predict New Fruit
prediction = model.predict([[4]])
# Display Result
print("Prediction =", prediction[0])
🟥 Expected Output
Prediction = Large
🟦 Step-by-Step Working of the Program
Step 1
Import SVC Class
│
▼
Step 2
Create Training Dataset (X)
│
▼
Step 3
Create Output Labels (y)
│
▼
Step 4
Create SVM Model
(kernel = "linear")
│
▼
Step 5
Train Model
(model.fit)
│
▼
Step 6
Predict New Data
(model.predict)
│
▼
Step 7
Display Prediction
🟩 How SVM Makes the Decision
Suppose the training data is:
| Weight | Category |
|---|---|
| 2 | Small |
| 3 | Small |
| 4 | Large |
| 5 | Large |
The SVM finds the best boundary:
Small Fruits Large Fruits
2 3 | 4 5
○------○------|------●------●
↑
Best Hyperplane
When a new fruit with weight = 4 kg is given:
- It lies on the Large side of the hyperplane.
- Therefore, the model predicts Large.
🟪 Advantages of SVM
- ✔ High accuracy
- ✔ Effective for classification problems
- ✔ Works well with high-dimensional data
- ✔ Handles both linear and non-linear data (using kernels)
- ✔ Less prone to overfitting
🟥 Limitations of SVM
- ❌ Training is slower for very large datasets
- ❌ Choosing the correct kernel can be difficult
- ❌ Sensitive to noisy data
- ❌ Requires careful parameter tuning
🌍 Real-Life Applications
- 🏥 Disease Diagnosis
- 📧 Spam Email Detection
- 😊 Face Recognition
- ✍️ Handwriting Recognition
- 💳 Credit Card Fraud Detection
- 🚗 Traffic Sign Recognition
- 📱 Image Classification
📝 Viva Questions
- What is Support Vector Machine (SVM)?
- What is a hyperplane in SVM?
- What are support vectors?
- What is the role of the kernel in SVM?
- What is the difference between Linear SVM and Non-Linear SVM?
- Why is SVM considered a powerful classification algorithm?
⭐ One-Line Revision
Support Vector Machine (SVM) is a supervised machine learning algorithm that classifies data by finding the optimal hyperplane with the maximum margin between different classes.
No comments:
Post a Comment