Desicion Tree Ipynb
Desicion Tree Ipynb
ipynb - Colaboratory
Decison Tree
We'll use the iris dataset to visualize how a decision tree works.
iris = load_iris()
iris.keys()
150 samples
3 labels: species of Iris (Iris setosa, Iris virginica and Iris versicolor)
Scikit learn only works if data is stored as numeric data, irrespective of it being a regression or a classification problem. It also requires the
arrays to be stored at numpy arrays for optimization. Since, this dataset is loaded from scikit learn, everything is appropriately formatted
iris['feature_names']
Assign the data and target to separate variables. x contains the features and y contains the label
https://colab.research.google.com/drive/11xVrqpEgeHcu9DfrErPhEsoqc5qPu70x#scrollTo=HnrpmO2mSgvI&printMode=true 1/6
08/12/2021, 13:14 decision_trees (1).ipynb - Colaboratory
X = iris.data[:,2 :] # considering only the petal length and petal width as the features
y = iris.target
DecisionTreeClassifier(max_depth=2)
Make predictions:
predictions=classifier.predict(x_test)
predictions
array([2, 1, 1, 2, 1, 1, 2, 2, 1, 0, 2, 2, 2, 2, 0, 1, 1, 0, 1, 1, 1, 0,
0, 0, 1, 0, 2, 0, 0, 2, 1, 0, 2, 0, 0, 0, 1, 1, 2, 0, 1, 1, 1, 1,
0])
https://colab.research.google.com/drive/11xVrqpEgeHcu9DfrErPhEsoqc5qPu70x#scrollTo=HnrpmO2mSgvI&printMode=true 2/6
08/12/2021, 13:14 decision_trees (1).ipynb - Colaboratory
[Text(133.92000000000002, 181.2, 'X[0] <= 2.45\ngini = 0.666\nsamples = 105\nvalue = [35, 33, 37]'),
Text(66.96000000000001, 108.72, 'gini = 0.0\nsamples = 35\nvalue = [35, 0, 0]'),
Text(200.88000000000002, 108.72, 'X[1] <= 1.7\ngini = 0.498\nsamples = 70\nvalue = [0, 33, 37]'),
Text(133.92000000000002, 36.23999999999998, 'gini = 0.111\nsamples = 34\nvalue = [0, 32, 2]'),
Text(267.84000000000003, 36.23999999999998, 'gini = 0.054\nsamples = 36\nvalue = [0, 1, 35]')]
Saving…
Performance measure
0.9333333333333333
array([[15, 0, 0],
[ 0, 16, 1],
https://colab.research.google.com/drive/11xVrqpEgeHcu9DfrErPhEsoqc5qPu70x#scrollTo=HnrpmO2mSgvI&printMode=true 3/6
08/12/2021, 13:14 decision_trees (1).ipynb - Colaboratory
[ 0, 2, 11]])
we can visualize the trained decision tree using the export_graphviz() method.
export_graphviz(
classifier,
out_file='iris_tree.dot',
Saving…
feature_names=iris.feature_names[2:],
class_names=iris.target_names,
rounded=True,
filled=True)
%ls
iris_tree.dot sample_data/
Now we can use the following command to convert our .dot file to the required format
https://colab.research.google.com/drive/11xVrqpEgeHcu9DfrErPhEsoqc5qPu70x#scrollTo=HnrpmO2mSgvI&printMode=true 4/6
08/12/2021, 13:14 decision_trees (1).ipynb - Colaboratory
Saving…
classifier.predict([[5, 1.5]])
array([1])
https://colab.research.google.com/drive/11xVrqpEgeHcu9DfrErPhEsoqc5qPu70x#scrollTo=HnrpmO2mSgvI&printMode=true 5/6
08/12/2021, 13:14 decision_trees (1).ipynb - Colaboratory
Saving…
https://colab.research.google.com/drive/11xVrqpEgeHcu9DfrErPhEsoqc5qPu70x#scrollTo=HnrpmO2mSgvI&printMode=true 6/6