-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iris data set , accuracy 98 , SVM.py
211 lines (129 loc) · 5.03 KB
/
Iris data set , accuracy 98 , SVM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# coding: utf-8
# ___
#
# <a href='http://www.datascribble.com'>
# ___
# # Support Vector Machines Project
#
# Welcome to your Support Vector Machine Project! Just follow along with the notebook and instructions below. We will be analyzing the famous iris data set!
#
# ## The Data
# For this series of lectures, we will be using the famous [Iris flower data set](http://en.wikipedia.org/wiki/Iris_flower_data_set).
#
# The Iris flower data set or Fisher's Iris data set is a multivariate data set introduced by Sir Ronald Fisher in the 1936 as an example of discriminant analysis.
#
# The data set consists of 50 samples from each of three species of Iris (Iris setosa, Iris virginica and Iris versicolor), so 150 total samples. Four features were measured from each sample: the length and the width of the sepals and petals, in centimeters.
#
# Here's a picture of the three different Iris types:
# In[1]:
# The Iris Setosa
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Image(url,width=300, height=300)
# In[3]:
# The Iris Versicolor
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/4/41/Iris_versicolor_3.jpg'
Image(url,width=300, height=300)
# In[5]:
# The Iris Virginica
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/9/9f/Iris_virginica.jpg'
Image(url,width=300, height=300)
# The iris dataset contains measurements for 150 iris flowers from three different species.
#
# The three classes in the Iris dataset:
#
# Iris-setosa (n=50)
# Iris-versicolor (n=50)
# Iris-virginica (n=50)
#
# The four features of the Iris dataset:
#
# sepal length in cm
# sepal width in cm
# petal length in cm
# petal width in cm
#
# ## Get the data
#
# **Use seaborn to get the iris data by using: iris = sns.load_dataset('iris') **
# In[15]:
import seaborn as sns
iris = sns.load_dataset('iris')
iris.head()
# Let's visualize the data and get you started!
#
# ## Exploratory Data Analysis
#
# Time to put your data viz skills to the test! Try to recreate the following plots, make sure to import the libraries you'll need!
#
# **Import some libraries you think you'll need.**
# In[11]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
get_ipython().magic('matplotlib inline')
# ** Create a pairplot of the data set. Which flower species seems to be the most separable?**
# In[17]:
iris.head()
sns.pairplot(iris , hue='species')
# **Create a kde plot of sepal_length versus sepal width for setosa species of flower.**
# In[21]:
setosa = iris[iris['species']=='setosa']
sns.kdeplot( setosa['sepal_width'], setosa['sepal_length'],
cmap="plasma", shade=True, shade_lowest=False)
# # Train Test Split
#
# ** Split your data into a training set and a testing set.**
# In[20]:
from sklearn.model_selection import train_test_split
# In[26]:
X=iris.drop('species' , axis=1)
y=iris['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
# # Train a Model
#
# Now its time to train a Support Vector Machine Classifier.
#
# **Call the SVC() model from sklearn and fit the model to the training data.**
# In[28]:
from sklearn.svm import SVC
# In[29]:
svm= SVC()
# In[30]:
svm.fit(X_train,y_train)
# ## Model Evaluation
#
# **Now get predictions from the model and create a confusion matrix and a classification report.**
# In[32]:
pred=svm.predict(X_test)
# In[34]:
from sklearn.metrics import classification_report,confusion_matrix
# In[37]:
print(confusion_matrix(y_test,pred))
# In[39]:
print(classification_report(y_test,pred))
svm.score(X_test,y_test)
# Wow! You should have noticed that your model was pretty good! Let's see if we can tune the parameters to try to get even better (unlikely, and you probably would be satisfied with these results in real like because the data set is quite small, but I just want you to practice using GridSearch.
# ## Gridsearch Practice
#
# ** Import GridsearchCV from SciKit Learn.**
# In[41]:
from sklearn.model_selection import GridSearchCV
# **Create a dictionary called param_grid and fill out some parameters for C and gamma.**
# In[42]:
param_grid={'C': [0.1,1, 10, 100], 'gamma': [1,0.1,0.01,0.001]}
# ** Create a GridSearchCV object and fit it to the training data.**
# In[43]:
grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=3)
grid.fit(X_train,y_train)
# ** Now take that grid model and create some predictions using the test set and create classification reports and confusion matrices for them. Were you able to improve?**
# In[44]:
grid_predictions = grid.predict(X_test)
# In[45]:
print(confusion_matrix(y_test,grid_predictions))
# In[46]:
print(classification_report(y_test,grid_predictions))
# You should have done about the same or exactly the same, this makes sense, there is basically just one point that is too noisey to grab, which makes sense, we don't want to have an overfit model that would be able to grab that.
# ## Great Job!