MAKKUVA SRUJAN                                                                   21131A4430
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
count_Class=pd.value_counts(df[" income"], sort= True)
count_Class.plot(kind= 'bar')
output:
from sklearn.preprocessing import LabelEncoder
le_income = LabelEncoder()
df[' income'] = le_income.fit_transform(df[' income'])
le_workclass = LabelEncoder()
df[' workclass'] = le_workclass.fit_transform(df[' workclass'])
le_education = LabelEncoder()
df[' education'] = le_education.fit_transform(df[' education'])
le_maritalstatus = LabelEncoder()
df[' marital-status'] = le_maritalstatus.fit_transform(df[' marital-status'])
le_occupation = LabelEncoder()
df[' occupation'] = le_occupation.fit_transform(df[' occupation'])
print("\nNow the Train data is :\n",df.head())
output:
MAKKUVA SRUJAN                                                           21131A4430
No_of_zeros= len(df[df[" income"]==0])
No_of_ones = len(df[df[" income"]==1])
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)
total= No_of_zeros + No_of_ones
zeros_percent= (No_of_zeros / total)*100
ones_percent= (No_of_ones / total)*100
print("Class 0 percentage = ", zeros_percent)
print("Class 1 percentage = ", ones_percent)
output:
#list of one indices
one_index= np.array(df[df[" income"]==1].index)
#getting the list of zero indices from the full dataset
zero_index= df[df[" income"]==0].index
#choosing random zero indices equal to the number of one indices
random_zero_indices= np.random.choice(zero_index, No_of_ones, replace= False)
random_zero_indices= np.array(random_zero_indices)
# concatenate one index and zero index to create a list of indices
undersampled_indices= np.concatenate([one_index, random_zero_indices])
#use the undersampled indices to build the undersampled_data dataframe
undersampled_data= df.iloc[undersampled_indices, :]
print(undersampled_data.head())
MAKKUVA SRUJAN                                                            21131A4430
output:
No_of_ones_sampled= len(undersampled_data[undersampled_data[" income"]== 1])
No_of_zeros_sampled = len(undersampled_data[undersampled_data[" income"]== 0])
print("The number of ones( income 1) are: ", No_of_ones_sampled)
print("The number of zeros( income 0) are: ", No_of_zeros_sampled)
total_sampled= No_of_ones_sampled + No_of_zeros_sampled
print("The total number of rows of both incomees are: ", total_sampled)
one_percent_sampled= (No_of_ones_sampled / total_sampled)*100
zero_percent_sampled= (No_of_zeros_sampled / total_sampled)*100
print(" income 0 percentage = ", zero_percent_sampled)
print(" income 1 percentage = ", one_percent_sampled)
#Check the data count now
count_sampled=pd.value_counts(undersampled_data[" income"], sort= True)
count_sampled.plot(kind= 'bar')
output:
MAKKUVA SRUJAN                                                         21131A4430
undersampled_data= undersampled_data.drop([" fnlwgt"," race"," relationship","
native-country"," sex"], axis= 1)
print(undersampled_data.head())
X= undersampled_data.iloc[:, [0,4]].values
y= undersampled_data[' income'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.25,
random_state= 0)
print("The split of the under_sampled data is as follows")
print("X_train: ", len(X_train))
print("X_test: ", len(X_test))
print("y_train: ", len(y_train))
print("y_test: ", len(y_test))
classifier= SVC(C= 1, kernel= 'linear', random_state= 0)
classifier.fit(X_train, y_train.ravel())
y_pred = classifier.predict(X_test)
cm1 = confusion_matrix(y_test, y_pred)
print(cm1)
from sklearn import metrics
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
output:
MAKKUVA SRUJAN                                                         21131A4430
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
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: virginica, Predicted: virginica
Wrong Prediction -   Actual: versicolor, Predicted: virginica
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: virginica, Predicted: virginica
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: versicolor, Predicted: versicolor
Correct Prediction   - Actual: setosa, Predicted: setosa
Correct Prediction   - Actual: setosa, Predicted: setosa