لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science Machine Learning Lesson 12: Decision Tree

A Decision Tree is a Flow Chart, and it is used in Decision making processes based on past experiences.

Note: In order to do this lesson, you need to have jupyter notebook installed on your computer.

Alternate for this is to use Google colab.
go to the url :google colab
No need to download any libraries. You just need to connect to the runtime and after that upload the required dataset and you're good to go.

In the example, a person will try to decide if he/she should play tennis or not.
Luckily our example person will decide whether to play tennis or not based on some certain conditions of the attributes.
Now, based on this data set, Python can create a decision tree that can be used to decide whether to play tennis or not.

In order to make a decision tree, all data has to be encoded numerically.
We have to convert the non numerical columns 'Outlook' and 'Temperature' and remaing other categorical columns into numerical values.

Python has sklearn module to plot a Decision Tree and in order to visualize the decision tree, pydotplus and graphviz modules are very useful.

The python code to plot and visualize a decision tree is given under

code

import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg

d = {'Sunny': 1, 'Overcast': 2, 'Rain': 3}
df['Outlook'] = df['Outlook'].map(d)
d = {'Cool': 1, 'Mild': 2, 'Hot': 3}
df['Temperature'] = df['Temperature'].map(d)
d = {'Normal': 1, 'High': 2}
df['Humidity'] = df['Humidity'].map(d)
d = {'Weak': 1, 'Strong': 2}
df['Wind'] = df['Wind'].map(d)
d = {'Yes': 1, 'No': 0}
df['Play'] = df['Play'].map(d)

features = ['Outlook', 'Temperature', 'Humidity', 'Wind']

X = df[features]
y = df['Play']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.
graph_from_dot_data(data)
graph.write_png
('mydecisiontree.png')
plt.figure(figsize=(10,20))

img=pltimg.
imgplot = plt.imshow(img)
plt.show()

The output will be

Click Here To See This AI- Based ML Model Live In Action.

Note: You can upload different datasets, encode their categorical features and further construct a Decision Tree.
You can use google colab platform since it is the faster and freely available jupyter runtime.

Explanation:

1. All the required libraries and the required dataset are loaded first.

2. Encoded all the categorical columns of the dataset to numerical values using map() method.

3. All the independent features(X) and dependent feature(Y) are taken seperately.

4. Decision Tree algorithm is applied on X and y.

5. After performing the algorithm, the Decision Tree is now visualized using pydotplus and graphviz module.

Note: if you have python installed on your pc you can install sklearn as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install sklearn
press enter
This command will install sklearn on your computer after which you can run on python.

Note: if you have python installed on your pc you can install pydotplus as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install pydotplus
press enter
This command will install pydotplus on your computer after which you can run on python.

Note: if you have python installed on your pc you can install graphviz as under.
Open Command Prompt from the start menu.
Inside the command prompt, type
pip install graphviz
press enter
This command will install graphviz on your computer after which you can run on python.