0
0
ML Pythonprogramming~5 mins

Support Vector Machine (SVM) in ML Python

Choose your learning style9 modes available
Introduction
Support Vector Machine helps us separate different groups in data by drawing a clear line or boundary between them.
When you want to classify emails as spam or not spam.
When you need to recognize handwritten digits.
When sorting fruits by type based on their features like size and color.
When detecting if a patient has a disease based on medical test results.
Syntax
ML Python
from sklearn import svm

model = svm.SVC(kernel='linear', C=1.0)
model.fit(X_train, y_train)
predictions = model.predict(X_test)
The 'kernel' decides the shape of the boundary line. 'linear' means a straight line.
The 'C' parameter controls how much you want to avoid mistakes on training data.
Examples
Creates an SVM model with a straight line boundary.
ML Python
model = svm.SVC(kernel='linear')
Creates an SVM model with a curved boundary to handle more complex data.
ML Python
model = svm.SVC(kernel='rbf')
Creates an SVM model with a polynomial boundary of degree 3.
ML Python
model = svm.SVC(kernel='poly', degree=3)
Sample Program
This program trains an SVM to separate two types of iris flowers using their features. It then tests the model and shows predictions and accuracy.
ML Python
from sklearn import svm
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load simple flower data
iris = load_iris()
X = iris.data
y = iris.target

# Use only two classes for simplicity
X = X[y != 2]
y = y[y != 2]

# Split data into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42)

# Create SVM model with linear kernel
model = svm.SVC(kernel='linear', C=1.0)

# Train the model
model.fit(X_train, y_train)

# Predict on test data
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)

print(f"Predictions: {predictions}")
print(f"Accuracy: {accuracy:.2f}")
OutputSuccess
Important Notes
SVM works best when classes are clearly separated.
Choosing the right kernel helps SVM handle different shapes of data.
The C parameter balances between a smooth boundary and fitting training data well.
Summary
SVM draws a boundary to separate groups in data.
You can choose different kernels for different data shapes.
SVM is good for clear and simple classification tasks.