0% found this document useful (0 votes)
13 views3 pages

PML Lab Exp 10

Uploaded by

rudrav728
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)
13 views3 pages

PML Lab Exp 10

Uploaded by

rudrav728
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
You are on page 1/ 3

Experiment 10: Support Vector Machine Classifier

Aim
Write a Python program to implement a Support Vector Machine (SVM) classifier to classify a dataset
and evaluate the accuracy.

Algorithm
1. Load the dataset from a CSV file.

2. Separate the dataset into features (input variables) and target (output variable).

3. Split the dataset into training and testing sets.

4. Initialize a Support Vector Machine (SVM) classifier.

5. Train the classifier on the training data.

6. Predict the target values for the testing set.

7. Evaluate the model using metrics such as accuracy, precision, and recall.

8. Plot the decision boundary and save it to a file.

Program
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix

data = pd.read_csv(’student_performance_svm.csv’)

X = data[[’Hours_Studied’, ’Previous_Score’]].values
y = data[’Pass’].values

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

model = SVC(kernel=’linear’)

model.fit(X_train, y_train)

1
AML311 - PML Lab

y_pred = model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)


precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print("Confusion Matrix:")
print(conf_matrix)

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.1),
np.arange(y_min, y_max, 0.1))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

plt.figure(figsize=(10, 6))
plt.contourf(xx, yy, Z, alpha=0.8, cmap=plt.cm.coolwarm)
plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors=’k’, marker=’o’, cmap=plt.cm.coolwarm)
plt.xlabel(’Hours Studied’)
plt.ylabel(’Previous Score’)
plt.title("SVM Decision Boundary")
plt.savefig(’svm_decision_boundary.png’)
plt.clf()

Result
The SVM classifier was successfully implemented to classify the dataset. The accuracy, precision, and
recall were calculated.

Viva Questions and Answers


1. What is a Support Vector Machine (SVM)?
SVM is a supervised machine learning algorithm used for classification and regression tasks. It works
by finding a hyperplane that best separates different classes in the data.

2. How does an SVM classify data?


SVM finds the optimal hyperplane that maximizes the margin between different classes. The data
points that are closest to the hyperplane are called support vectors, and these help define the decision
boundary.

3. What is the purpose of the kernel in an SVM?


The kernel function allows SVM to perform classification in a higher-dimensional space, enabling it
to handle non-linearly separable data. Common kernels include linear, polynomial, and RBF (Radial
Basis Function).

2 CEK
AML311 - PML Lab

4. What does the ’linear’ kernel do in SVM?


The linear kernel is used when the data is linearly separable, meaning it can be separated by a
straight line or hyperplane. It performs classification by constructing a linear decision boundary.

5. What is a hyperplane in the context of SVM?


A hyperplane is a decision boundary that separates different classes of data. In a two-dimensional
space, it is a line, and in a three-dimensional space, it is a plane.

6. What are support vectors?


Support vectors are the data points that lie closest to the decision boundary. They are critical in
determining the position and orientation of the hyperplane.

7. What is the difference between a linear and non-linear SVM?


A linear SVM uses a straight hyperplane to separate classes, while a non-linear SVM uses a kernel
function (such as RBF or polynomial) to map data into higher dimensions, allowing more complex
decision boundaries.

8. How is the decision boundary determined in SVM?


The decision boundary is determined by the support vectors, and it is placed to maximize the margin
between the classes.

9. What are the advantages of using SVM?


SVM is effective for high-dimensional spaces, works well with a clear margin of separation, is effective
even when the number of features exceeds the number of samples, and can use different kernel
functions to handle non-linear data.

10. What are the disadvantages of using SVM?


SVM can be computationally intensive, especially for large datasets, is sensitive to the choice of
kernel and its parameters, and does not perform well with overlapping classes or noisy data.

3 CEK

You might also like