ML Week-12
ML Week-12
WEEK-12
AIM: Write a program to implement Support Vector Machine algorithm to classify the iris
data set. Print both correct and wrong predictions.
Description:
Support Vector Machine or SVM is one of the most popular Supervised Learning
algorithms, which is used for Classification as well as Regression problems. However,
primarily, it is used for Classification problems in Machine Learning.
The goal of the SVM algorithm is to create the best line or decision boundary that can
segregate n-dimensional space into classes so that we can easily put the new data point in
the correct category in the future. This best decision boundary is called a hyperplane.
PROGRAM:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from mlxtend.plotting import plot_confusion_matrix
from sklearn.metrics import confusion_matrix
df = pd.read_csv("/content/income_evaluation.csv")
df.head()
output:
MAKKUVA SRUJAN 21131A4430
output:
MAKKUVA SRUJAN 21131A4430
print("The number of ID's don't get credit card are: ", No_of_zeros)
print("The number of ID's get credit card are: ", No_of_ones)
print(undersampled_data.head())
MAKKUVA SRUJAN 21131A4430
output:
PROGRAM:
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report,
confusion_matrix
# Load the Iris dataset
Iris = datasets.load_iris()
X = Iris.data # Feature matrix
y = Iris.target # Target vector (species labels)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
print("X_train: ", len(X_train))
print("X_test: ", len(X_test))
print("y_train: ", len(y_train))
print("y_test: ", len(y_test))
# Create an SVM classifier with a linear kernel
clf = SVC(kernel='poly')
# Fit the classifier to the training data
clf.fit(X_train, y_train)
# Make predictions on the test data
y_pred = clf.predict(X_test)
print("\nClassification Report:")
print(classification_report(y_test, y_pred))
print("\nConfusion Matrix:")
print(confusion_matrix(y_test, y_pred))
# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Print both correct and wrong predictions
for i in range(len(y_test)):
if y_pred[i] == y_test[i]:
print(f"Correct Prediction - Actual: {Iris.target_names[y_test[i]]},
Predicted: {Iris.target_names[y_pred[i]]}")
else:
print(f"Wrong Prediction - Actual: {Iris.target_names[y_test[i]]},
Predicted: {Iris.target_names[y_pred[i]]}")
output:
MAKKUVA SRUJAN 21131A4430
Confusion Matrix:
[[19 0 0]
[ 0 12 1]
[ 0 0 13]]
Accuracy: 0.9777777777777777