Support Vector Machine
Support Vector Machine
[2]: plt.rcParams['figure.figsize']=[19,8]
[7]: iris=load_iris()
[8]: dir(iris)
[8]: ['DESCR',
'data',
'data_module',
'feature_names',
'filename',
'frame',
'target',
'target_names']
[9]: iris_df=pd.DataFrame(data=iris.data,columns=iris.feature_names)
iris_df.head()
[9]: sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)
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
1
[11]: iris_df.head()
[11]: sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \
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
target
0 0
1 0
2 0
3 0
4 0
[12]: iris_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 sepal length (cm) 150 non-null float64
1 sepal width (cm) 150 non-null float64
2 petal length (cm) 150 non-null float64
3 petal width (cm) 150 non-null float64
4 target 150 non-null int32
dtypes: float64(4), int32(1)
memory usage: 5.4 KB
[13]: iris.target_names
[14]: iris_df.duplicated().sum()
[14]: 1
[15]: iris_df.drop_duplicates(inplace=True)
[16]: iris_df.duplicated().sum()
[16]: 0
2
plt.show()
3
[32]: sns.scatterplot(data=iris_setosa, x='sepal length (cm)', y='sepal width (cm)',␣
↪s=150, label='Setosa')
[33]: sns.boxplot(iris_df)
plt.show()
4
[34]: # obtain the first quartile
Q1 = iris_df.quantile(0.25)
# obtain the third quartile
Q3 = iris_df.quantile(0.75)
# obtain the IQR
IQR= Q3-Q1
#print the IQR
print(IQR)
[37]: sns.boxplot(iris_df)
plt.show()
5
[40]: X =iris_df.loc[:,:'petal width (cm)'].values
y =iris_df.loc[:, 'target'].values
[41]: y
[41]: 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, 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])
[45]: SVC(kernel='linear')
6
[46]: 0.9827586206896551
[48]: y_predict
[48]: array([0, 2, 0, 2, 1, 0, 0, 2, 0, 1, 1, 1, 1, 2, 0, 2, 0, 0, 0, 1, 2, 1,
0, 0, 2, 2, 2, 2, 1])
[49]: y_test
[49]: array([0, 1, 0, 2, 1, 0, 0, 2, 0, 1, 1, 1, 1, 1, 0, 2, 0, 0, 0, 1, 2, 1,
0, 0, 2, 2, 2, 2, 1])
[52]: cm
[ ]: