02 - Decision Tree Classification On Iris Dataset
02 - Decision Tree Classification On Iris Dataset
Import Libraries
In [1]:
1 import numpy as np
2 import pandas as pd
3 from sklearn.tree import DecisionTreeClassifier
In [2]:
1 data = pd.read_csv("Iris.csv")
2 data.head(3)
Out[2]:
In [3]:
1 data.info()
<class 'pandas.core.frame.DataFrame'>
X is data and Y is target data i.e species
In [4]:
1 X = data[['SepalLengthCm','SepalWidthCm','PetalLengthCm','PetalWidthCm']].values
2 X[:5]
Out[4]:
In [5]:
1 Y = data['Species']
2 Y[:5]
Out[5]:
0 Iris-setosa
1 Iris-setosa
2 Iris-setosa
3 Iris-setosa
4 Iris-setosa
Training Model
In [6]:
In [7]:
Out[7]:
DecisionTreeClassifier(criterion='entropy', max_depth=4)
In [8]:
1 SpeciesTree.fit(X_trainset, Y_trainset)
Out[8]:
DecisionTreeClassifier(criterion='entropy', max_depth=4)
Prediction
In [9]:
1 predTree = SpeciesTree.predict(X_testset)
2 predTree [0:5]
Out[9]:
In [10]:
1 Y_testset[0:5]
Out[10]:
114 Iris-virginica
62 Iris-versicolor
33 Iris-setosa
107 Iris-virginica
7 Iris-setosa
In [11]:
Out[12]:
Prediction-1
In [13]:
1 X_new = [[6.3,3.0,1.3,0.2]]
2 predTree = SpeciesTree.predict(X_new)
3 predTree
Out[13]:
array(['Iris-setosa'], dtype=object)
Prediction-2
In [14]:
1 X_new = [[5.4,2.8,2.9,1.5]]
2 predTree = SpeciesTree.predict(X_new)
3 predTree
Out[14]:
array(['Iris-versicolor'], dtype=object)
Prediction-3
In [15]:
1 X_new = [[5.4,2.8,2.9,0.5]]
2 predTree = SpeciesTree.predict(X_new)
3 predTree
Out[15]:
array(['Iris-versicolor'], dtype=object)