0% found this document useful (0 votes)
15 views2 pages

EXP 08 (ML) - Shri

The document discusses using a support vector machine (SVM) classifier on a social network advertising dataset. It loads and preprocesses the data, splits it into training and test sets, fits an SVM to the training set, makes predictions on the test set, and visualizes the results. Key steps include feature scaling, fitting a linear kernel SVM to the training data, predicting on the test set, and plotting decision boundaries and data points on 2D graphs to visualize the model's performance on both training and test sets.
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)
15 views2 pages

EXP 08 (ML) - Shri

The document discusses using a support vector machine (SVM) classifier on a social network advertising dataset. It loads and preprocesses the data, splits it into training and test sets, fits an SVM to the training set, makes predictions on the test set, and visualizes the results. Key steps include feature scaling, fitting a linear kernel SVM to the training data, predicting on the test set, and plotting decision boundaries and data points on 2D graphs to visualize the model's performance on both training and test sets.
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/ 2

8/20/23, 1:00 AM EXP_08(ML).

ipynb - Colaboratory

Name-: Shriyash Gharat

Roll_No -:

# Support Vector Machine


# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from google.colab import files

# Importing the datasets


uploaded=files.upload()
datasets = pd.read_csv('Social_Network_Ads.csv')
X = datasets.iloc[:, [2,3]].values
Y = datasets.iloc[:, 4].values

Choose Files Social_Network_Ads.csv


Social_Network_Ads.csv(text/csv) - 10926 bytes, last modified: 1/18/2018 - 100% done
Saving Social_Network_Ads.csv to Social_Network_Ads (2).csv

# Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split


X_Train, X_Test, Y_Train, Y_Test = train_test_split(X, Y, test_size = 0.25, random_state = 0)

# Feature Scaling

from sklearn.preprocessing import StandardScaler


sc_X = StandardScaler()
X_Train = sc_X.fit_transform(X_Train)
X_Test = sc_X.transform(X_Test)

# Fitting the classifier into the Training set

from sklearn.svm import SVC


classifier = SVC(kernel = 'linear', random_state = 0)
classifier.fit(X_Train, Y_Train)

# Predicting the test set results

Y_Pred = classifier.predict(X_Test)

# Making the Confusion Matrix

from sklearn.metrics import confusion_matrix


cm = confusion_matrix(Y_Test, Y_Pred)

# Visualising the Training set results

from matplotlib.colors import ListedColormap


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.title('Support Vector Machine (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

https://colab.research.google.com/drive/1U0hYcVA2oFZLMlRLEcQpWdeDnk0-dUqc#scrollTo=TRihGC9BJPXF&printMode=true 1/3
8/20/23, 1:00 AM EXP_08(ML).ipynb - Colaboratory

<ipython-input-15-b68905a3ef8f>:12: UserWarning: *c* argument looks like a single


plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],

# Visualising the Test set results

from matplotlib.colors import ListedColormap


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)
plt.title('Support Vector Machine (Test set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

<ipython-input-16-39ca6ab02970>:12: UserWarning: *c* argument looks like a single


plt.scatter(X_Set[Y_Set == j, 0], X_Set[Y_Set == j, 1],

https://colab.research.google.com/drive/1U0hYcVA2oFZLMlRLEcQpWdeDnk0-dUqc#scrollTo=TRihGC9BJPXF&printMode=true 2/3

You might also like