PML Lab Exp 10
PML Lab Exp 10
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).
7. Evaluate the model using metrics such as accuracy, precision, and recall.
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
model = SVC(kernel=’linear’)
model.fit(X_train, y_train)
1
AML311 - PML Lab
y_pred = model.predict(X_test)
print(f"Accuracy: {accuracy:.2f}")
print(f"Precision: {precision:.2f}")
print(f"Recall: {recall:.2f}")
print("Confusion Matrix:")
print(conf_matrix)
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.
2 CEK
AML311 - PML Lab
3 CEK