You are on page 1of 7

Assignment-7 (CSL5402)

Name :- Kommireddy.Manikanta sai


Roll No :- 1906036
Branch :- CSE-1

WAP to classify the type of flower. The features of the flower type are given in the
iris dataset format. To perform this task, student can only use SVM classifier. Report the
accuracy difference between different kernals (Poly, linear, RBF) on iris dataset.
Code with outputs:-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import classification_report
In [2]:

df=pd.read_csv("iris.csv")
In [3]:

df.head
Out[3]:

<bound method NDFrame.head of sepal_length sepal_width petal_length petal_width


plant
0 5.1 3.5 1.4 0.2 0
1 4.9 3.0 1.4 0.2 0
2 4.7 3.2 1.3 0.2 0
3 4.6 3.1 1.5 0.2 0
4 5.0 3.6 1.4 0.2 0
.. ... ... ... ... ...
145 6.7 3.0 5.2 2.3 2
146 6.3 2.5 5.0 1.9 2
147 6.5 3.0 5.2 2.0 2
148 6.2 3.4 5.4 2.3 2
149 5.9 3.0 5.1 1.8 2

[150 rows x 5 columns]>


In [4]:
y=df.plant
In [5]:

X=df
In [6]:

X=X.drop(columns={"plant"})
In [7]:

X
Out[7]:

sepal_length sepal_width petal_length petal_width

0 5.1 3.5 1.4 0.2

1 4.9 3.0 1.4 0.2

2 4.7 3.2 1.3 0.2

3 4.6 3.1 1.5 0.2

4 5.0 3.6 1.4 0.2

... ... ... ... ...

145 6.7 3.0 5.2 2.3

146 6.3 2.5 5.0 1.9

147 6.5 3.0 5.2 2.0

148 6.2 3.4 5.4 2.3

149 5.9 3.0 5.1 1.8

150 rows × 4 columns


In [8]:

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)

kernel=poly
In [9]:

m=SVC(kernel='poly')
In [10]:

m.fit(X_train,y_train)
Out[10]:

SVC(kernel='poly')
In [11]:

y_pred=m.predict(X_test)
In [12]:

print(classification_report(y_test,y_pred))
precision recall f1-score support

0 1.00 1.00 1.00 11


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 6

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

kernel=linear
In [13]:

m=SVC(kernel='linear')
In [14]:

m.fit(X_train,y_train)
Out[14]:

SVC(kernel='linear')
In [15]:

y_pred=m.predict(X_test)print(classification_report(y_test,y_pred))
precision recall f1-score support

0 1.00 1.00 1.00 11


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 6

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

kernel=RBF
In [16]:

m=SVC(kernel='rbf')
In [17]:

m.fit(X_train,y_train)
Out[17]:

SVC()
In [18]:
y_pred=m.predict(X_test)print(classification_report(y_test,y_pred))
precision recall f1-score support

0 1.00 1.00 1.00 11


1 1.00 1.00 1.00 13
2 1.00 1.00 1.00 6

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

WAP to classify the purchase of a social media user with feature


The dataset is in social_advformat and student should only use SVM classifier. Report the
classification result with Poly and RBF kernel.
Code:-
In [19]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
In [20]:

d=pd.read_csv("Social_Network_Ads.csv")d.head()
Out[20]:

User ID Gender Age EstimatedSalary Purchased

0 15624510 Male 19 19000 0

1 15810944 Male 35 20000 0

2 15668575 Female 26 43000 0

3 15603246 Female 27 57000 0

4 15804002 Male 19 76000 0

In [21]:

d=d.drop(columns={"User ID"})d.head()
Out[21]:
Gender Age EstimatedSalary Purchased

0 Male 19 19000 0

1 Male 35 20000 0

2 Female 26 43000 0

3 Female 27 57000 0

4 Male 19 76000 0

In [22]:

l=d['Gender'].unique()idx=d['Gender']==l[0]
In [23]:

d['Gender'].loc[~idx]=0
C:\Users\manikantasai\anaconda3\lib\site-packages\pandas\core\indexing.py:1637:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-


docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self._setitem_single_block(indexer, value, name)
In [24]:

d['Gender'].loc[idx]=1
In [25]:

d.head()
Out[25]:

Gender Age EstimatedSalary Purchased

0 1 19 19000 0

1 1 35 20000 0

2 0 26 43000 0

3 0 27 57000 0

4 1 19 76000 0

In [26]:

X=d
In [27]:

X=X.drop(columns='Purchased')
In [28]:

y=d['Purchased']
In [29]:

X
Out[29]:

Gender Age EstimatedSalary

0 1 19 19000

1 1 35 20000

2 0 26 43000

3 0 27 57000

4 1 19 76000

... ... ... ...

395 0 46 41000

396 1 51 23000

397 0 50 20000

398 1 36 33000

399 0 49 36000

400 rows × 3 columns


In [30]:

y
Out[30]:

0 0
1 0
2 0
3 0
4 0
..
395 1
396 1
397 1
398 0
399 1
Name: Purchased, Length: 400, dtype: int64
In [31]:

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0)

kernel=poly
In [33]:
m=SVC(kernel='poly')m.fit(X_train,y_train)
Out[33]:

SVC(kernel='poly')
In [34]:

y_pred=m.predict(X_test)print(classification_report(y_test,y_pred))
precision recall f1-score support

0 0.81 0.97 0.88 58


1 0.82 0.41 0.55 22

accuracy 0.81 80
macro avg 0.81 0.69 0.71 80
weighted avg 0.81 0.81 0.79 80

kernel=rbf
In [35]:

m=SVC(kernel='rbf')m.fit(X_train,y_train)
Out[35]:

SVC()
In [36]:

y_pred=m.predict(X_test)print(classification_report(y_test,y_pred))
precision recall f1-score support

0 0.82 0.97 0.89 58


1 0.83 0.45 0.59 22

accuracy 0.82 80
macro avg 0.83 0.71 0.74 80
weighted avg 0.83 0.82 0.81 80

You might also like