0% found this document useful (0 votes)
2 views7 pages

Svmdoc

The document outlines an implementation of a Support Vector Machine (SVM) using Python, detailing the steps from data loading to visualization. It includes the necessary libraries, dataset preparation, model training, prediction, and result visualization. The implementation successfully demonstrates the SVM classifier on a user dataset.

Uploaded by

s.shaflafathima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Svmdoc

The document outlines an implementation of a Support Vector Machine (SVM) using Python, detailing the steps from data loading to visualization. It includes the necessary libraries, dataset preparation, model training, prediction, and result visualization. The implementation successfully demonstrates the SVM classifier on a user dataset.

Uploaded by

s.shaflafathima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

EX NO: 12

SUPPORT VECTOR MACHINE

AIM :

To implement support vector machine using python

ALGORITHM :
1. Import numpy, pandas and matplotlib package.
2. Load the user dataset.
3. Analysis data.
4. Collect all the independent and dependent variable
5. Splitting the dataset into training and test set.
6. Fitting the SVM classifier to the training set
7. Predicting the test set result
8. Creating the Confusion matrix
9. Visualizing the training set result
10. Visualizing the test set result

PROGRAM :

# importing libraries

import numpy as nm

import matplotlib.pyplot as mtp

import pandas as pd

# importing datasets

data_set= pd.read_csv('User_Data.csv')

#Extracting Independent and dependent Variable

x= data_set.iloc[:, [2,3]].values

y= data_set.iloc[:, 4].values
# Splitting the dataset into training 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

st_x= StandardScaler()

x_train= st_x.fit_transform(x_train)

x_test= st_x.transform(x_test)

#Fitting the SVM classifier to the training set

from sklearn.svm import SVC # "Support vector classifier"

classifier = SVC(kernel='linear', random_state=0)

classifier.fit(x_train, y_train)

#Predicting the test set result

y_pred= classifier.predict(x_test)

#Creating the Confusion matrix

from sklearn.metrics import confusion_matrix

cm= confusion_matrix(y_test, y_pred)

#Visualizing the training set result

from matplotlib.colors import ListedColormap

x_set, y_set = x_train, y_train


x1, x2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max() +

1, step =0.01),

nm.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))

mtp.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(),

x2.ravel()]).T).reshape(x1.shape),

alpha = 0.75, cmap = ListedColormap(('red', 'green')))

mtp.xlim(x1.min(), x1.max())

mtp.ylim(x2.min(), x2.max())

for i, j in enumerate(nm.unique(y_set)):

mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],

c = ListedColormap(('red', 'green'))(i), label = j)

mtp.title('SVM classifier (Training set)')

mtp.xlabel('Age')

mtp.ylabel('Estimated Salary')

mtp.legend()

mtp.show()

<ipython-input-16-48af1f85b2be>:11: UserWarning: *c* argument looks like a single numeric

RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in

case its length matches with *x* & *y*. Please use the *color* keyword-argument or provide a

2D array with a single row if you intend to specify the same RGB or RGBA value for all

points.

mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],


#Visualizing the test set result

from matplotlib.colors import ListedColormap

x_set, y_set = x_test, y_test

x1, x2 = nm.meshgrid(nm.arange(start = x_set[:, 0].min() - 1, stop = x_set[:, 0].max()

1, step =0.01),

nm.arange(start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1, step = 0.01))

mtp.contourf(x1, x2, classifier.predict(nm.array([x1.ravel(),

x2.ravel()]).T).reshape(x1.shape),

alpha = 0.75, cmap = ListedColormap(('red','green' )))

mtp.xlim(x1.min(), x1.max())

mtp.ylim(x2.min(), x2.max())

for i, j in enumerate(nm.unique(y_set)):

mtp.scatter(x_set[y_set == j, 0], x_set[y_set == j, 1],

c = ListedColormap(('red', 'green'))(i), label = j)

mtp.title('SVM classifier (Test set)')

mtp.xlabel('Age')

mtp.ylabel('Estimated Salary')

mtp.legend()

mtp.show()
OUTPUT :
RESULT :

Thus support vector machine implemented successfully

You might also like