In [1]:
from sklearn import datasets
iris = datasets.load_iris() # load iris data set

In [2]:
x = iris.data[:,2:] # the attributes
y = iris.target # the target variable

In [5]:
def get_species(length, width):
    species=['setona', 'versicolor', 'virginica']
    
    if length <= 2.45: # 1
        return species[0]
    else:
        if width <= 1.75: # 2, left
            if length <= 4.95: # 3, left
                if width <= 1.65: # 4, left
                    return species[1]
                else:
                    return species[2]
            else:
                if width <= 1.55: # 4, right
                    return species[2]
                else:
                    if length <= 5.45: # 5
                        return species[1]
                    else:
                        return species[2]
        else: # 2, right
            return species[2]
            
for length, width in zip(x[:, 0], x[:, 1]):
    print("({}, {}) -> {}".format(length, width, get_species(length, width)))


(1.4, 0.2) -> setona
(1.4, 0.2) -> setona
(1.3, 0.2) -> setona
(1.5, 0.2) -> setona
(1.4, 0.2) -> setona
(1.7, 0.4) -> setona
(1.4, 0.3) -> setona
(1.5, 0.2) -> setona
(1.4, 0.2) -> setona
(1.5, 0.1) -> setona
(1.5, 0.2) -> setona
(1.6, 0.2) -> setona
(1.4, 0.1) -> setona
(1.1, 0.1) -> setona
(1.2, 0.2) -> setona
(1.5, 0.4) -> setona
(1.3, 0.4) -> setona
(1.4, 0.3) -> setona
(1.7, 0.3) -> setona
(1.5, 0.3) -> setona
(1.7, 0.2) -> setona
(1.5, 0.4) -> setona
(1.0, 0.2) -> setona
(1.7, 0.5) -> setona
(1.9, 0.2) -> setona
(1.6, 0.2) -> setona
(1.6, 0.4) -> setona
(1.5, 0.2) -> setona
(1.4, 0.2) -> setona
(1.6, 0.2) -> setona
(1.6, 0.2) -> setona
(1.5, 0.4) -> setona
(1.5, 0.1) -> setona
(1.4, 0.2) -> setona
(1.5, 0.1) -> setona
(1.2, 0.2) -> setona
(1.3, 0.2) -> setona
(1.5, 0.1) -> setona
(1.3, 0.2) -> setona
(1.5, 0.2) -> setona
(1.3, 0.3) -> setona
(1.3, 0.3) -> setona
(1.3, 0.2) -> setona
(1.6, 0.6) -> setona
(1.9, 0.4) -> setona
(1.4, 0.3) -> setona
(1.6, 0.2) -> setona
(1.4, 0.2) -> setona
(1.5, 0.2) -> setona
(1.4, 0.2) -> setona
(4.7, 1.4) -> versicolor
(4.5, 1.5) -> versicolor
(4.9, 1.5) -> versicolor
(4.0, 1.3) -> versicolor
(4.6, 1.5) -> versicolor
(4.5, 1.3) -> versicolor
(4.7, 1.6) -> versicolor
(3.3, 1.0) -> versicolor
(4.6, 1.3) -> versicolor
(3.9, 1.4) -> versicolor
(3.5, 1.0) -> versicolor
(4.2, 1.5) -> versicolor
(4.0, 1.0) -> versicolor
(4.7, 1.4) -> versicolor
(3.6, 1.3) -> versicolor
(4.4, 1.4) -> versicolor
(4.5, 1.5) -> versicolor
(4.1, 1.0) -> versicolor
(4.5, 1.5) -> versicolor
(3.9, 1.1) -> versicolor
(4.8, 1.8) -> virginica
(4.0, 1.3) -> versicolor
(4.9, 1.5) -> versicolor
(4.7, 1.2) -> versicolor
(4.3, 1.3) -> versicolor
(4.4, 1.4) -> versicolor
(4.8, 1.4) -> versicolor
(5.0, 1.7) -> versicolor
(4.5, 1.5) -> versicolor
(3.5, 1.0) -> versicolor
(3.8, 1.1) -> versicolor
(3.7, 1.0) -> versicolor
(3.9, 1.2) -> versicolor
(5.1, 1.6) -> versicolor
(4.5, 1.5) -> versicolor
(4.5, 1.6) -> versicolor
(4.7, 1.5) -> versicolor
(4.4, 1.3) -> versicolor
(4.1, 1.3) -> versicolor
(4.0, 1.3) -> versicolor
(4.4, 1.2) -> versicolor
(4.6, 1.4) -> versicolor
(4.0, 1.2) -> versicolor
(3.3, 1.0) -> versicolor
(4.2, 1.3) -> versicolor
(4.2, 1.2) -> versicolor
(4.2, 1.3) -> versicolor
(4.3, 1.3) -> versicolor
(3.0, 1.1) -> versicolor
(4.1, 1.3) -> versicolor
(6.0, 2.5) -> virginica
(5.1, 1.9) -> virginica
(5.9, 2.1) -> virginica
(5.6, 1.8) -> virginica
(5.8, 2.2) -> virginica
(6.6, 2.1) -> virginica
(4.5, 1.7) -> virginica
(6.3, 1.8) -> virginica
(5.8, 1.8) -> virginica
(6.1, 2.5) -> virginica
(5.1, 2.0) -> virginica
(5.3, 1.9) -> virginica
(5.5, 2.1) -> virginica
(5.0, 2.0) -> virginica
(5.1, 2.4) -> virginica
(5.3, 2.3) -> virginica
(5.5, 1.8) -> virginica
(6.7, 2.2) -> virginica
(6.9, 2.3) -> virginica
(5.0, 1.5) -> virginica
(5.7, 2.3) -> virginica
(4.9, 2.0) -> virginica
(6.7, 2.0) -> virginica
(4.9, 1.8) -> virginica
(5.7, 2.1) -> virginica
(6.0, 1.8) -> virginica
(4.8, 1.8) -> virginica
(4.9, 1.8) -> virginica
(5.6, 2.1) -> virginica
(5.8, 1.6) -> virginica
(6.1, 1.9) -> virginica
(6.4, 2.0) -> virginica
(5.6, 2.2) -> virginica
(5.1, 1.5) -> virginica
(5.6, 1.4) -> virginica
(6.1, 2.3) -> virginica
(5.6, 2.4) -> virginica
(5.5, 1.8) -> virginica
(4.8, 1.8) -> virginica
(5.4, 2.1) -> virginica
(5.6, 2.4) -> virginica
(5.1, 2.3) -> virginica
(5.1, 1.9) -> virginica
(5.9, 2.3) -> virginica
(5.7, 2.5) -> virginica
(5.2, 2.3) -> virginica
(5.0, 1.9) -> virginica
(5.2, 2.0) -> virginica
(5.4, 2.3) -> virginica
(5.1, 1.8) -> virginica

petal length = 4.5 cm petal width = 1.51 cm Should predict class 1 (iris versicolor)


In [6]:
get_species(4.5, 1.51)


Out[6]:
'versicolor'

In [ ]: