0% found this document useful (0 votes)
14 views4 pages

SVM Implementation in Python

The document provides three Python programs demonstrating the implementation of Support Vector Machines (SVM). The first program builds an SVM from scratch using a hard-margin linear approach, the second uses scikit-learn for a linear SVM, and the third applies SVM on the Iris dataset with an RBF kernel. Each program includes steps for data preparation, model training, parameter extraction, and visualization of results.

Uploaded by

rockysr90
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

SVM Implementation in Python

The document provides three Python programs demonstrating the implementation of Support Vector Machines (SVM). The first program builds an SVM from scratch using a hard-margin linear approach, the second uses scikit-learn for a linear SVM, and the third applies SVM on the Iris dataset with an RBF kernel. Each program includes steps for data preparation, model training, parameter extraction, and visualization of results.

Uploaded by

rockysr90
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Implementation of Support Vector Machine (SVM) in Python

Program 1

#SVM From Scratch (Hard-Margin, Linear)


import numpy as np
import [Link] as plt
from cvxopt import matrix, solvers
# ----------- Step 1: Create toy dataset -----------
X = [Link]([
[2, 2],
[2, 3],
[3, 3],
[5, 5],
[6, 6],
[7, 8]
])
y = [Link]([-1, -1, -1, 1, 1, 1]) # labels must be -1 or +1
n_samples, n_features = [Link]
# ----------- Step 2: Compute Gram matrix -----------
K = [Link](X, X.T) # Linear kernel
# ----------- Step 3: Setup QP problem for cvxopt -----------
P = matrix([Link](y, y) * K, tc='d')
q = matrix(-[Link](n_samples), tc='d')
G = matrix(-[Link](n_samples), tc='d')
h = matrix([Link](n_samples), tc='d')
A = matrix([Link](float), (1, n_samples))
b = matrix(0.0)
# Solve QP problem
sol = [Link](P, q, G, h, A, b)
alphas = [Link](sol['x'])
# ----------- Step 4: Extract support vectors -----------
threshold = 1e-5
support_vector_indices = alphas > threshold
alphas_sv = alphas[support_vector_indices]
X_sv = X[support_vector_indices]
y_sv = y[support_vector_indices]
print("Support Vectors:\n", X_sv)
# ----------- Step 5: Compute weights (w) and bias (b) -----------
w = [Link](alphas_sv[:, None] * y_sv[:, None] * X_sv, axis=0)
# Compute bias using any support vector
b = [Link]([y_sv[i] - [Link](w, X_sv[i]) for i in range(len(alphas_sv))])
print("Weight vector:", w)
print("Bias:", b)
# ----------- Step 6: Prediction function -----------
def predict(X_new):
return [Link]([Link](X_new, w) + b)

# ----------- Step 7: Visualization -----------


[Link](X[:, 0], X[:, 1], c=y, cmap=[Link], s=60, edgecolors='k')

# Plot support vectors


[Link](X_sv[:, 0], X_sv[:, 1], s=120, facecolors='none', edgecolors='k')

# Plot decision boundary


x1 = [Link](0, 8, 100)
x2 = -(w[0] * x1 + b) / w[1]
[Link](x1, x2, 'k-')

# Plot margins
margin = 1 / [Link](w)
x2_margin_up = -(w[0] * x1 + b - 1) / w[1]
x2_margin_down = -(w[0] * x1 + b + 1) / w[1]
[Link](x1, x2_margin_up, 'k--')
[Link](x1, x2_margin_down, 'k--')

[Link]("SVM From Scratch (Linear, Hard-Margin)")


[Link]("x1")
[Link]("x2")
[Link]()
Program 2

# SVM with scikit-learn (Linear Kernel)


import numpy as np
import [Link] as plt
from [Link] import SVC
# ----------- Step 1: Create toy dataset -----------
X = [Link]([
[2, 2],
[2, 3],
[3, 3],
[5, 5],
[6, 6],
[7, 8]
])
y = [Link]([-1, -1, -1, 1, 1, 1]) # labels must be -1 or +1
# ----------- Step 2: Train Linear SVM -----------
clf = SVC(kernel='linear', C=1e5) # big C ≈ hard-margin
[Link](X, y)
# ----------- Step 3: Get model parameters -----------
w = clf.coef_[0]
b = clf.intercept_[0]
print("Weight vector:", w)
print("Bias:", b)
print("Support Vectors:\n", clf.support_vectors_)
# ----------- Step 4: Visualization -----------
[Link](X[:, 0], X[:, 1], c=y, cmap=[Link], s=60, edgecolors='k')
# Highlight support vectors
[Link](clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
s=120, facecolors='none', edgecolors='k')
# Plot decision boundary
x1 = [Link](0, 8, 100)
x2 = -(w[0] * x1 + b) / w[1]
[Link](x1, x2, 'k-')
# Plot margins
margin = 1 / [Link](w)
x2_margin_up = -(w[0] * x1 + b - 1) / w[1]
x2_margin_down = -(w[0] * x1 + b + 1) / w[1]
[Link](x1, x2_margin_up, 'k--')
[Link](x1, x2_margin_down, 'k--')
[Link]("SVM with scikit-learn (Linear, Hard-Margin)")
[Link]("x1")
[Link]("x2")
[Link]()
Program 3

#SVM on Iris Dataset with scikit-learn


import numpy as np
import [Link] as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import SVC
from [Link] import classification_report, confusion_matrix
# Load dataset (Iris)
iris = datasets.load_iris()
X = [Link][:, :2] # Only take first 2 features for visualization
y = [Link]
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Standardize features
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = [Link](X_test)
# Train SVM model (RBF kernel)
svm_clf = SVC(kernel='rbf', C=1.0, gamma=0.5)
svm_clf.fit(X_train, y_train)
# Predictions
y_pred = svm_clf.predict(X_test)
# Evaluation
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred,
target_names=iris.target_names))
# -------- Visualization --------
# Create meshgrid for plotting decision boundaries
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = [Link]([Link](x_min, x_max, 500),
[Link](y_min, y_max, 500))

Z = svm_clf.predict(np.c_[[Link](), [Link]()])
Z = [Link]([Link])

[Link](xx, yy, Z, alpha=0.3, cmap=[Link])


[Link](X_train[:, 0], X_train[:, 1], c=y_train, cmap=[Link], edgecolors='k')
[Link]("Feature 1 (sepal length)")
[Link]("Feature 2 (sepal width)")
[Link]("SVM Decision Boundary (Iris dataset)")
[Link]()

You might also like