Professional Documents
Culture Documents
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]:
X=df
In [6]:
X=X.drop(columns={"plant"})
In [7]:
X
Out[7]:
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
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
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
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
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]:
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
d['Gender'].loc[idx]=1
In [25]:
d.head()
Out[25]:
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]:
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
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
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
accuracy 0.82 80
macro avg 0.83 0.71 0.74 80
weighted avg 0.83 0.82 0.81 80