01.04.2015 Views

1FfUrl0

1FfUrl0

1FfUrl0

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2<br />

We are using Matplotlib; it is the most well-known plotting package for Python. We<br />

present the code to generate the top-left plot. The code for the other plots is similar to<br />

the following code:<br />

from matplotlib import pyplot as plt<br />

from sklearn.datasets import load_iris<br />

import numpy as np<br />

# We load the data with load_iris from sklearn<br />

data = load_iris()<br />

features = data['data']<br />

feature_names = data['feature_names']<br />

target = data['target']<br />

for t,marker,c in zip(xrange(3),">ox","rgb"):<br />

# We plot each class on its own to get different colored markers<br />

plt.scatter(features[target == t,0],<br />

features[target == t,1],<br />

marker=marker,<br />

c=c)<br />

Building our first classification model<br />

If the goal is to separate the three types of flower, we can immediately make a few<br />

suggestions. For example, the petal length seems to be able to separate Iris Setosa<br />

from the other two flower species on its own. We can write a little bit of code to<br />

discover where the cutoff is as follows:<br />

plength = features[:, 2]<br />

# use numpy operations to get setosa features<br />

is_setosa = (labels == 'setosa')<br />

# This is the important step:<br />

max_setosa =plength[is_setosa].max()<br />

min_non_setosa = plength[~is_setosa].min()<br />

print('Maximum of setosa: {0}.'.format(max_setosa))<br />

print('Minimum of others: {0}.'.format(min_non_setosa))<br />

This prints 1.9 and 3.0. Therefore, we can build a simple model: if the petal length<br />

is smaller than two, this is an Iris Setosa flower; otherwise, it is either Iris Virginica or<br />

Iris Versicolor.<br />

if features[:,2] < 2: print 'Iris Setosa'<br />

else: print 'Iris Virginica or Iris Versicolour'<br />

[ 35 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!