0% found this document useful (0 votes)
137 views4 pages

3 SVM - Jupyter Notebook

The document discusses building a support vector machine (SVM) classifier on the Iris dataset to predict flower species (setosa, versicolor, vergenica) based on sepal and petal attributes. It splits the dataset into training and test sets, fits an SVM with linear and polynomial kernels to the training set, predicts the test set, and calculates accuracy scores. The linear kernel achieves 94.7% accuracy while the polynomial kernel achieves 93.3% accuracy.

Uploaded by

venkatesh m
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)
137 views4 pages

3 SVM - Jupyter Notebook

The document discusses building a support vector machine (SVM) classifier on the Iris dataset to predict flower species (setosa, versicolor, vergenica) based on sepal and petal attributes. It splits the dataset into training and test sets, fits an SVM with linear and polynomial kernels to the training set, predicts the test set, and calculates accuracy scores. The linear kernel achieves 94.7% accuracy while the polynomial kernel achieves 93.3% accuracy.

Uploaded by

venkatesh m
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/ 4

In 

[ ]: # SVM when output variable have discrete column

In [2]: import numpy as np


import matplotlib.pyplot as plt
import pandas as pd

In [3]: iris = pd.read_csv("D:\\Course\\Python\\Datasets\\iris.csv")


iris

...

In [4]: iris_setosa = iris.iloc[0:50,0:5] # 50


iris_versicolor = iris.iloc[50:100,0:5] # 50
iris_vergenica = iris.iloc[100:150,0:5] # 50

In [5]: iris_versicolor

...

In [7]: # first 25 observation from setosa data - df1



df1 = pd.DataFrame(iris_setosa.iloc[0:25,0:5])

In [8]: # first 25 obesrvation from Versicolor data - df2



df2 = pd.DataFrame(iris_versicolor.iloc[0:25,0:5])
#df2

In [9]: # first 25 observations from Vergenica dataset - df3



df3 = pd.DataFrame(iris_vergenica.iloc[0:25,0:5])

In [10]: # training dataset which contains 25 from all 3 datasets by using concat

iris_train = pd.concat([df1,df2,df3])

In [11]: iris_train

...

In [12]: # Dividing the input and output variable for Training Dataset - xtrain and Y trai
X_train = iris_train.iloc[:,0:4]
X_train
y_train = iris_train.iloc[:,4]
y_train
X_train

...
In [13]: # next 25 observation from Setosa dataset
df1 = pd.DataFrame(iris_setosa.iloc[25:50,0:5])
# next 25 observation from Versicolor dataset
df2 = pd.DataFrame(iris_versicolor.iloc[25:50,0:5])
# next 25 observation from Vergenica dataset
df3 = pd.DataFrame(iris_vergenica.iloc[25:50,0:5])

In [14]: # testing datset by concat the all 3 datasets ( df1,df2,df3)


iris_test= pd.concat([df1,df2,df3])
iris_test
...

In [15]: iris_test.shape

Out[15]: (75, 5)

In [16]: X_test = iris_test.iloc[:,0:4]


X_test
y_test = iris_test.iloc[:,4]
y_test

...

In [26]: # X_train and X_test are the dataset for training and Testing
#y_train ,y_test are target predictor for Training and Testing Dataset

In [17]: X_train, y_train,X_test,y_test

...

In [18]: # Fitting SVM to the Training set


# we dont have radial Method in python so we have to use linear method for linear
# Importing libraries and Method
from sklearn.svm import SVC
# Creating An alogrithm using imported method names
classifier = SVC(kernel = 'linear')

#Apply alogrithm on Training dataset (xtrain , ytrain) by using .fitI()

classifier.fit(X_train, y_train)

...

In [19]: # Predicting the Test set results by applying on only input variables of
# testind dataset (Xtest)

y_pred = classifier.predict(X_test)

In [20]: y_pred

...
In [22]: y_test

...

In [23]: # Making the Confusion Matrix by validating the predicted and Actual Values

# importing required libraries
from sklearn.metrics import confusion_matrix


cm = confusion_matrix(y_test, y_pred)

In [24]: cm

Out[24]: array([[25, 0, 0],

[ 0, 24, 1],

[ 0, 3, 22]], dtype=int64)

In [25]: from sklearn.metrics import accuracy_score



Accuracy_Score = accuracy_score(y_test, y_pred)

In [26]: Accuracy_Score

Out[26]: 0.9466666666666667

In [29]: # WORKING ON KERNET TRICK


In [27]: # Fitting SVM to the Training set


# we dont have radial Method in python so we have to use linear method for linear
from sklearn.svm import SVC

classifier1 = SVC(kernel = 'poly')

classifier1.fit(X_train, y_train)

...

In [32]: # Predicting the Test set results


y_pred1 = classifier1.predict(X_test)
y_pred1

...

In [33]: # Making the Confusion Matrix


from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred1)
cm

...
In [34]: from sklearn.metrics import accuracy_score
Accuracy_Score = accuracy_score(y_test, y_pred1)

In [35]: Accuracy_Score

Out[35]: 0.9333333333333333

In [33]: # Kernel Trick for non linear graph


# 1)polynomial - poly
# 2) Gaussian - rbf
# 3) Sigmoid - sigmoid

You might also like