Today we will explore the famous Iris flower dataset.

“The earth laughs in flowers.” -Ralph Waldo Emerson

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.
iris types

Get the data

Seaborn and Sklearn have the dataset, so less hassle for us.

1
2
import seaborn as sns
iris = sns.load_dataset('iris')

Exploratory Data Analysis

Ok, we saw the beautiful flowers, now let’s see if the flowers are separable.

1
2
3
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
1
2
# Setosa is the most separable.
sns.pairplot(iris,hue='species',palette='muted')
<seaborn.axisgrid.PairGrid at 0x7effd6a5ed50>

png

As we can see, the setosa iris is very clearly different from the others. But also distinguishing versicolor from virginica is also clear.

Let’s create an kdeplot of sepal_length versus sepal width for setosa species of flower.

1
2
3
setosa = iris[iris['species']=='setosa']
sns.kdeplot( setosa['sepal_width'], setosa['sepal_length'],
cmap="plasma", shade=True, shade_lowest=False)
<matplotlib.axes._subplots.AxesSubplot at 0x7effc21361d0>

png

Let’s do some predictions with SVM

Spliting test and training data

1
from sklearn.cross_validation import train_test_split
1
2
3
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.

1
from sklearn.svm import SVC
1
svc_model = SVC()
1
svc_model.fit(X_train,y_train)
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

Model Evaluation

Let’ see how well our model predicts. We will also create a confusion matrix and a classification report.

1
predictions = svc_model.predict(X_test)
1
from sklearn.metrics import classification_report,confusion_matrix
1
print(confusion_matrix(y_test,predictions))
[[16  0  0]
 [ 0 14  0]
 [ 0  2 13]]
1
print(classification_report(y_test,predictions))
             precision    recall  f1-score   support

     setosa       1.00      1.00      1.00        15
 versicolor       1.00      0.94      0.97        18
  virginica       0.92      1.00      0.96        12

avg / total       0.96      0.96      0.96        45

96% WOW! The only dataset where I get such a good accuracy.
Ok, Let’s see if we can tune the parameters to try to get even better. Unlikely that would happen, but for practice we will use an GridSearch.

Gridsearch

1
from sklearn.grid_search import GridSearchCV

Let’s create our dictionary of C & gamma values

1
param_grid = {'C': [0.1,1, 10, 100], 'gamma': [1,0.1,0.01,0.001]}

Ok, let’s do this

1
2
grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=2)
grid.fit(X_train,y_train)
Fitting 3 folds for each of 16 candidates, totalling 48 fits
[CV] C=0.1, gamma=1 ..................................................
[CV] ......................................... C=0.1, gamma=1 -   0.0s
[CV] C=0.1, gamma=1 ..................................................
[CV] ......................................... C=0.1, gamma=1 -   0.0s
[CV] C=0.1, gamma=1 ..................................................
[CV] ......................................... C=0.1, gamma=1 -   0.0s
[CV] C=0.1, gamma=0.1 ................................................
[CV] ....................................... C=0.1, gamma=0.1 -   0.0s
[CV] C=0.1, gamma=0.1 ................................................
[CV] ....................................... C=0.1, gamma=0.1 -   0.0s
[CV] C=0.1, gamma=0.1 ................................................
[CV] ....................................... C=0.1, gamma=0.1 -   0.0s
[CV] C=0.1, gamma=0.01 ...............................................
[CV] ...................................... C=0.1, gamma=0.01 -   0.0s
[CV] C=0.1, gamma=0.01 ...............................................
[CV] ...................................... C=0.1, gamma=0.01 -   0.0s
[CV] C=0.1, gamma=0.01 ...............................................
[CV] ...................................... C=0.1, gamma=0.01 -   0.0s
[CV] C=0.1, gamma=0.001 ..............................................
[CV] ..................................... C=0.1, gamma=0.001 -   0.0s
[CV] C=0.1, gamma=0.001 ..............................................
[CV] ..................................... C=0.1, gamma=0.001 -   0.0s
[CV] C=0.1, gamma=0.001 ..............................................
[CV] ..................................... C=0.1, gamma=0.001 -   0.0s
[CV] C=1, gamma=1 ....................................................
[CV] ........................................... C=1, gamma=1 -   0.0s
[CV] C=1, gamma=1 ....................................................
[CV] ........................................... C=1, gamma=1 -   0.0s
[CV] C=1, gamma=1 ....................................................
[CV] ........................................... C=1, gamma=1 -   0.0s
[CV] C=1, gamma=0.1 ..................................................
[CV] ......................................... C=1, gamma=0.1 -   0.0s
[CV] C=1, gamma=0.1 ..................................................
[CV] ......................................... C=1, gamma=0.1 -   0.0s
[CV] C=1, gamma=0.1 ..................................................
[CV] ......................................... C=1, gamma=0.1 -   0.0s
[CV] C=1, gamma=0.01 .................................................
[CV] ........................................ C=1, gamma=0.01 -   0.0s
[CV] C=1, gamma=0.01 .................................................
[CV] ........................................ C=1, gamma=0.01 -   0.0s
[CV] C=1, gamma=0.01 .................................................
[CV] ........................................ C=1, gamma=0.01 -   0.0s
[CV] C=1, gamma=0.001 ................................................
[CV] ....................................... C=1, gamma=0.001 -   0.0s
[CV] C=1, gamma=0.001 ................................................
[CV] ....................................... C=1, gamma=0.001 -   0.0s
[CV] C=1, gamma=0.001 ................................................
[CV] ....................................... C=1, gamma=0.001 -   0.0s
[CV] C=10, gamma=1 ...................................................
[CV] .......................................... C=10, gamma=1 -   0.0s
[CV] C=10, gamma=1 ...................................................
[CV] .......................................... C=10, gamma=1 -   0.0s
[CV] C=10, gamma=1 ...................................................
[CV] .......................................... C=10, gamma=1 -   0.0s
[CV] C=10, gamma=0.1 .................................................
[CV] ........................................ C=10, gamma=0.1 -   0.0s
[CV] C=10, gamma=0.1 .................................................
[CV] ........................................ C=10, gamma=0.1 -   0.0s
[CV] C=10, gamma=0.1 .................................................
[CV] ........................................ C=10, gamma=0.1 -   0.0s
[CV] C=10, gamma=0.01 ................................................
[CV] ....................................... C=10, gamma=0.01 -   0.0s
[CV] C=10, gamma=0.01 ................................................
[CV] ....................................... C=10, gamma=0.01 -   0.0s
[CV] C=10, gamma=0.01 ................................................
[CV] ....................................... C=10, gamma=0.01 -   0.0s
[CV] C=10, gamma=0.001 ...............................................
[CV] ...................................... C=10, gamma=0.001 -   0.0s
[CV] C=10, gamma=0.001 ...............................................
[CV] ...................................... C=10, gamma=0.001 -   0.0s
[CV] C=10, gamma=0.001 ...............................................
[CV] ...................................... C=10, gamma=0.001 -   0.0s
[CV] C=100, gamma=1 ..................................................
[CV] ......................................... C=100, gamma=1 -   0.0s
[CV] C=100, gamma=1 ..................................................
[CV] ......................................... C=100, gamma=1 -   0.0s
[CV] C=100, gamma=1 ..................................................
[CV] ......................................... C=100, gamma=1 -   0.0s
[CV] C=100, gamma=0.1 ................................................
[CV] ....................................... C=100, gamma=0.1 -   0.0s
[CV] C=100, gamma=0.1 ................................................
[CV] ....................................... C=100, gamma=0.1 -   0.0s
[CV] C=100, gamma=0.1 ................................................
[CV] ....................................... C=100, gamma=0.1 -   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ...................................... C=100, gamma=0.01 -   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ...................................... C=100, gamma=0.01 -   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ...................................... C=100, gamma=0.01 -   0.0s
[CV] C=100, gamma=0.001 ..............................................
[CV] ..................................... C=100, gamma=0.001 -   0.0s
[CV] C=100, gamma=0.001 ..............................................
[CV] ..................................... C=100, gamma=0.001 -   0.0s
[CV] C=100, gamma=0.001 ..............................................
[CV] ..................................... C=100, gamma=0.001 -   0.0s


[Parallel(n_jobs=1)]: Done  40 tasks       | elapsed:    0.6s
[Parallel(n_jobs=1)]: Done  48 out of  48 | elapsed:    0.7s finished





GridSearchCV(cv=None, error_score='raise',
       estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False),
       fit_params={}, iid=True, n_jobs=1,
       param_grid={'C': [0.1, 1, 10, 100], 'gamma': [1, 0.1, 0.01, 0.001]},
       pre_dispatch='2*n_jobs', refit=True, scoring=None, verbose=2)

Ok, let’s see if we did better.

1
grid_predictions = grid.predict(X_test)
1
print(confusion_matrix(y_test,grid_predictions))
[[16  0  0]
 [ 0 14  0]
 [ 0  3 12]]
1
print(classification_report(y_test,grid_predictions))
             precision    recall  f1-score   support

     setosa       1.00      1.00      1.00        15
 versicolor       1.00      0.89      0.94        18
  virginica       0.86      1.00      0.92        12

avg / total       0.96      0.96      0.96        45

Same, however the data for the 4% improvement is too noisy and we would be overfitting our data.