#import libraries and load iris dataset
import pandas as pd
from sklearn.datasets import load_iris
iris=load_iris()
#To get the features in iris
dir(iris)
['DESCR',
'data',
'feature_names',
'filename',
'frame',
'target',
'target_names']
iris.data
array([[5.1, 3.5, 1.4, 0.2],
[4.9, 3. , 1.4, 0.2],
[4.7, 3.2, 1.3, 0.2],
[4.6, 3.1, 1.5, 0.2],
[5. , 3.6, 1.4, 0.2],
[5.4, 3.9, 1.7, 0.4],
[4.6, 3.4, 1.4, 0.3],
[5. , 3.4, 1.5, 0.2],
[4.4, 2.9, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.1],
[5.4, 3.7, 1.5, 0.2],
[4.8, 3.4, 1.6, 0.2],
[4.8, 3. , 1.4, 0.1],
[4.3, 3. , 1.1, 0.1],
[5.8, 4. , 1.2, 0.2],
[5.7, 4.4, 1.5, 0.4],
[5.4, 3.9, 1.3, 0.4],
[5.1, 3.5, 1.4, 0.3],
[5.7, 3.8, 1.7, 0.3],
[5.1, 3.8, 1.5, 0.3],
[5.4, 3.4, 1.7, 0.2],
[5.1, 3.7, 1.5, 0.4],
[4.6, 3.6, 1. , 0.2],
[5.1, 3.3, 1.7, 0.5],
[4.8, 3.4, 1.9, 0.2],
[5. , 3. , 1.6, 0.2],
[5. , 3.4, 1.6, 0.4],
[5.2, 3.5, 1.5, 0.2],
[5.2, 3.4, 1.4, 0.2],
[4.7, 3.2, 1.6, 0.2],
[4.8, 3.1, 1.6, 0.2],
[5.4, 3.4, 1.5, 0.4],
[5.2, 4.1, 1.5, 0.1],
[5.5, 4.2, 1.4, 0.2],
[4.9, 3.1, 1.5, 0.2],
[5. , 3.2, 1.2, 0.2],
[5.5, 3.5, 1.3, 0.2],
[4.9, 3.6, 1.4, 0.1],
[4.4, 3. , 1.3, 0.2],
[5.1, 3.4, 1.5, 0.2],
[5. , 3.5, 1.3, 0.3],
[4.5, 2.3, 1.3, 0.3],
[4.4, 3.2, 1.3, 0.2],
[5. , 3.5, 1.6, 0.6],
[5.1, 3.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.3],
[6.7, 3.3, 5.7, 2.5],
[6.7, 3. , 5.2, 2.3],
[6.3, 2.5, 5. , 1.9],
[6.5, 3. , 5.2, 2. ],
[6.2, 3.4, 5.4, 2.3],
[5.9, 3. , 5.1, 1.8]])
iris.feature_names
['sepal length (cm)',
'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']
#Create a dataframe by taking values of feature_names
df=pd.DataFrame(iris.data,columns=iris.feature_names)
df.head()
df.shape
(150, 4)
iris.target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])
[k for k in df.columns if df[k].isnull().sum()>0]
df['target']=iris.target
df.head()
iris.target_names
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')
df['flower_names']=df.target.apply(lambda x:iris.target_names[x])
df
import matplotlib.pyplot as plt
df0=df[df.target==0]
df1=df[df.target==1]
df2=df[df.target==2]
df0.head()
df1.head()
df2.head()
plt.xlabel('sepal length (cm)')
plt.ylabel('sepal width (cm)')
plt.scatter(df0['sepal length (cm)'],df0['sepal width (cm)'],color='green',marker='+')
plt.scatter(df1['sepal length (cm)'],df1['sepal width (cm)'],color='blue',marker='+')
#plt.scatter(df2['sepal length (cm)'],df2['sepal width (cm)'],color='red',marker='+')
plt.show()
plt.xlabel('petal length (cm)')
plt.ylabel('petal width (cm)')
plt.scatter(df0['petal length (cm)'],df0['petal width (cm)'],color='green',marker='+')
plt.scatter(df1['petal length (cm)'],df1['petal width (cm)'],color='blue',marker='+')
plt.show()
X=df.drop(['target','flower_names'],axis='columns')
X.head()
y=df.target
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)
print(X_test)
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
63 6.1 2.9 4.7 1.4
128 6.4 2.8 5.6 2.1
7 5.0 3.4 1.5 0.2
86 6.7 3.1 4.7 1.5
95 5.7 3.0 4.2 1.2
127 6.1 3.0 4.9 1.8
79 5.7 2.6 3.5 1.0
61 5.9 3.0 4.2 1.5
52 6.9 3.1 4.9 1.5
31 5.4 3.4 1.5 0.4
129 7.2 3.0 5.8 1.6
88 5.6 3.0 4.1 1.3
120 6.9 3.2 5.7 2.3
140 6.7 3.1 5.6 2.4
27 5.2 3.5 1.5 0.2
32 5.2 4.1 1.5 0.1
11 4.8 3.4 1.6 0.2
70 5.9 3.2 4.8 1.8
93 5.0 2.3 3.3 1.0
6 4.6 3.4 1.4 0.3
72 6.3 2.5 4.9 1.5
17 5.1 3.5 1.4 0.3
46 5.1 3.8 1.6 0.2
118 7.7 2.6 6.9 2.3
99 5.7 2.8 4.1 1.3
57 4.9 2.4 3.3 1.0
96 5.7 2.9 4.2 1.3
92 5.8 2.6 4.0 1.2
56 6.3 3.3 4.7 1.6
81 5.5 2.4 3.7 1.0
print(y_test)
63 1
128 2
7 0
86 1
95 1
127 2
79 1
61 1
52 1
31 0
129 2
88 1
120 2
140 2
27 0
32 0
11 0
70 1
93 1
6 0
72 1
17 0
46 0
118 2
99 1
57 1
96 1
92 1
56 1
81 1
Name: target, dtype: int32
#Create SVM model
from sklearn.svm import SVC
obj=SVC()
#Trained the model
obj.fit(X_train,y_train)
SVC()
y_pred=obj.predict(X_test)
y_pred
array([1, 2, 0, 1, 1, 2, 1, 1, 1, 0, 2, 1, 2, 2, 0, 0, 0, 2, 1, 0, 1, 0,
0, 2, 1, 1, 1, 1, 1, 1])
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,y_pred))
0.9666666666666667
from sklearn.metrics import confusion_matrix
print(confusion_matrix(y_test,y_pred))
[[ 8 0 0]
[ 0 15 1]
[ 0 0 6]]
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc