Sie sind auf Seite 1von 4

9/21/2018 komal_DT3_Iris

DECISION TREE - EXAMPLE 2 - IRIS DB


In [1]: import numpy as np
import pandas as pd

import seaborn as sns


sns.set(style="white", color_codes=True)

from sklearn.model_selection import train_test_split

from sklearn.tree import DecisionTreeClassifier


from sklearn import metrics

In [2]: location = r"D:\KOMAL\SIMPLILEARN\MY COURSES\IN PROGRESS\DATA SCIENCE WITH PYT


HON\Live class downloads\Aug 11 Sat - Sep 15 Sat - Attending\datasets\iris.cs
v"

In [3]: # load the training data from iris data set


df_iris = pd.read_csv(location)
df_iris.head()

Out[3]:
sepal_length sepal_width petal_length petal_width class

0 5.1 3.5 1.4 0.2 Iris-setosa

1 4.9 3.0 1.4 0.2 Iris-setosa

2 4.7 3.2 1.3 0.2 Iris-setosa

3 4.6 3.1 1.5 0.2 Iris-setosa

4 5.0 3.6 1.4 0.2 Iris-setosa

file:///D:/KOMAL/SIMPLILEARN/MY%20COURSES/IN%20PROGRESS/My%20Codes_ML_DS/pdf%20conversion/htmls/komal_DT3_Iris.html 1/4
9/21/2018 komal_DT3_Iris

In [4]: sns.pairplot(data=df_iris, hue='class', size=3)

Out[4]: <seaborn.axisgrid.PairGrid at 0x2af74a89d30>

In [5]: # copy the predictor variables into X and responses in y


X = df_iris[['sepal_length','sepal_width', 'petal_length', 'petal_width']]
y = df_iris['class']

In [6]: # split the data into training and test data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, rando
m_state=24)

In [7]: clf = DecisionTreeClassifier()


clf = clf.fit(X_train, y_train)

file:///D:/KOMAL/SIMPLILEARN/MY%20COURSES/IN%20PROGRESS/My%20Codes_ML_DS/pdf%20conversion/htmls/komal_DT3_Iris.html 2/4
9/21/2018 komal_DT3_Iris

In [8]: clf

Out[8]: DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,


max_features=None, max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, presort=False, random_state=None,
splitter='best')

In [9]: y_pred = clf.predict(X_test)

In [10]: metrics.confusion_matrix(y_test, y_pred)

Out[10]: array([[15, 0, 0],


[ 0, 11, 1],
[ 0, 0, 18]], dtype=int64)

In [11]: # accuracy score


metrics.accuracy_score(y_test, y_pred)

Out[11]: 0.9777777777777777

In [12]: # classification report


print(metrics.classification_report(y_test, y_pred))

precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 15


Iris-versicolor 1.00 0.92 0.96 12
Iris-virginica 0.95 1.00 0.97 18

avg / total 0.98 0.98 0.98 45

In [13]: print(clf.feature_importances_)

[0.00716039 0.02148118 0.07852564 0.89283279]

In [14]: import graphviz


from sklearn.tree import export_graphviz

In [15]: dot_data = export_graphviz(clf,


out_file=None,
feature_names=df_iris.drop('class' , 1).column
s,
class_names=y.unique(),
filled=True,
rounded=True,
special_characters=True)

In [16]: graph = graphviz.Source(dot_data)

file:///D:/KOMAL/SIMPLILEARN/MY%20COURSES/IN%20PROGRESS/My%20Codes_ML_DS/pdf%20conversion/htmls/komal_DT3_Iris.html 3/4
9/21/2018 komal_DT3_Iris

In [17]: graph.render("D:\\KOMAL\\SIMPLILEARN\\MY COURSES\\IN PROGRESS\\graphviz output


s\\iris")

Out[17]: 'D:\\KOMAL\\SIMPLILEARN\\MY COURSES\\IN PROGRESS\\graphviz outputs\\iris.pdf'

In [18]: graph

Out[18]: petal_width ≤ 0.8


gini = 0.665
samples = 105
value = [35, 38, 32]
class = Iris-versicolor
False
True

petal_width ≤ 1.65
gini = 0.0
gini = 0.496
samples = 35
samples = 70
value = [35, 0, 0]
value = [0, 38, 32]
class = Iris-setosa
class = Iris-versicolor

petal_length ≤ 4.95 petal_length ≤ 4.85


gini = 0.139 gini = 0.064
samples = 40 samples = 30
value = [0, 37, 3] value = [0, 1, 29]
class = Iris-versicolor class = Iris-virginica

sepal_length ≤ 6.05 sepal_width ≤ 3.1


gini = 0.0 gini = 0.0
gini = 0.375 gini = 0.375
samples = 36 samples = 26
samples = 4 samples = 4
value = [0, 36, 0] value = [0, 0, 26]
value = [0, 1, 3] value = [0, 1, 3]
class = Iris-versicolor class = Iris-virginica
class = Iris-virginica class = Iris-virginica

petal_length ≤ 5.05
gini = 0.0 gini = 0.0 gini = 0.0
gini = 0.5
samples = 2 samples = 3 samples = 1
samples = 2
value = [0, 0, 2] value = [0, 0, 3] value = [0, 1, 0]
value = [0, 1, 1]
class = Iris-virginica class = Iris-virginica class = Iris-versicolor
class = Iris-versicolor

gini = 0.0 gini = 0.0


samples = 1 samples = 1
value = [0, 0, 1] value = [0, 1, 0]
class = Iris-virginica class = Iris-versicolor

file:///D:/KOMAL/SIMPLILEARN/MY%20COURSES/IN%20PROGRESS/My%20Codes_ML_DS/pdf%20conversion/htmls/komal_DT3_Iris.html 4/4