Exp 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Machine Learning 21BEC505

Experiment-6
Objective: Implementation of Support Vector Machine (SVMs)
Task #1
Code:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix,accuracy_score
from matplotlib.colors import ListedColormap

dataset = pd.read_csv(r"E:\Jay\NIRMA\Sem6\ML\Exp6\Social_Network_Ads.csv")

X = dataset.iloc[:,[2,3]].values
y = dataset.iloc[:,4].values

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=1/3,random_state=0)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
classifier = SVC(kernel='linear',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))

x_set, y_set = X_train, y_train


x1, x2 = np.meshgrid(np.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step=0.01),
np.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))
plt.contourf(x1, x2, classifier.predict(np.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(x1.min(), x1.max())
plt.ylim(x2.min(), x2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],c = ListedColormap(('red', 'green'))(i), label = j)
plt.legend()
plt.show()

x_set, y_set = X_test, y_test


x1, x2 = np.meshgrid(np.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() + 1, step=0.01),
np.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))
plt.contourf(x1, x2, classifier.predict(np.array([x1.ravel(), x2.ravel()]).T).reshape(x1.shape),
alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(x1.min(), x1.max())
plt.ylim(x2.min(), x2.max())
for i, j in enumerate(np.unique(y_set)):
plt.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1], c = ListedColormap(('red', 'green'))(i), label = j)
Machine Learning 21BEC505

plt.legend()
plt.show()

classifier = SVC(kernel='poly',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(kernel='rbf',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=0.1,kernel='rbf',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=1,kernel='rbf',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=10,kernel='rbf',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)

cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=100,kernel='rbf',random_state=0)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=10,kernel='rbf',random_state=0,gamma=0.1)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=10,kernel='rbf',random_state=0,gamma=1)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=10,kernel='rbf',random_state=0,gamma=10)
classifier.fit(X_train,y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))
classifier = SVC(C=10,kernel='rbf',random_state=0,gamma=100)
classifier.fit(X_train,y_train)
Machine Learning 21BEC505

y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test,y_pred)
print(accuracy_score(y_test,y_pred))

Output:
Machine Learning 21BEC505

Exercise:

1. Use SVM as a classifier and predict the traffic.

Code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from matplotlib.colors import ListedColormap
data=pd.read_csv(r"E:\Jay\NIRMA\Sem6\ML\Exp6\dodgers.data",header=None)
print('Raw Data: \n', data.head())
data.columns = ['date_time', 'Cars']
data['date_time'] = pd.to_datetime(data['date_time'],
infer_datetime_format=True, errors='coerce')
Date = data.date_time.dt.date
Time = data.date_time.dt.time
data = data.reindex(['Date', 'Time', *data.columns], axis=1).assign(Date = Date, Time = Time)
del data['date_time']
print('\n Parsed Data: \n', data.head())
data['Date'] = [str(d)[:10].replace("-", "") for d in data['Date']]
data['Time'] = [str(t)[:9].replace(":", "") for t in data['Time']]
data = data[data['Cars'] != -1]
traffic_map = {"light": 0, "medium": 1, "heavy": 2}
data['Traffic'] = data['Cars'].apply(lambda x: "heavy" if x >= 27 else "medium" if x >= 9 else "light")
data['Traffic'] = data['Traffic'].map(traffic_map)
X = data.iloc[:, [0, 1]].values
y = data.iloc[:, 3].values
column = data.columns.values[:2]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10, random_state=101)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
classifier = SVC(kernel='linear', random_state=26)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
cm = confusion_matrix(y_test, y_pred)
print(cm)
# Visulaize output (trained set)
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() -1, stop = X_set[:,0].max() +1, step = 1),
np.arange(start = X_set[:, 1].min() -1, stop = X_set[:, 1].max() +1, step = 0.5))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(),
X2.ravel()]).T).reshape(X1.shape), alpha = 0.68, cmap = ListedColormap(('red', 'green', 'blue')) )
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i,j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], color =
ListedColormap(('red','green', 'blue'))(i), label=next(key for key, value in traffic_map.items() if value == j))
Machine Learning 21BEC505

plt.title('SVM (Training Set)')


plt.xlabel(column[0])
plt.ylabel(column[1])
plt.legend()
plt.show()
from sklearn.metrics import accuracy_score
# Visualize test results (prediction)
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() -1, stop = X_set[:, 0].max() +1, step = 1),
np.arange(start = X_set[:, 1].min() -1, stop = X_set[:, 1].max() +1, step = 0.5))
plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.68,
cmap = ListedColormap(('red', 'green', 'blue')) )
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i,j in enumerate(np.unique(y_set)):
plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], color =
ListedColormap(('red','green', 'blue'))(i), label=next(key for key, value in traffic_map.items() if value == j))
plt.title('SVM (Test Set)')
plt.xlabel(column[0])
plt.ylabel(column[1])
plt.legend()
plt.show()
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

Output:
Machine Learning 21BEC505

Conclusion:
From this experiment, I understood about Support Vector Machine and furthermore about how to execute
SVM Classifier in python. Support vectors are used in SVM to find the hyperplane, and we can use this
hyperplane to predict the data point's label. By altering parameters like gamma, C, and the kernel, I was also
able to improve the model's accuracy. For the given dataset, we have implemented a Support Vector Machine
Classifier. Our model has an initial accuracy of 83 percent. The model's accuracy was then increased to 91.79
percent by adjusting kernel, C, and gamma parameters.

You might also like