#Three lines to make our compiler able to draw:
import sys
import matplotlib
matplotlib.use('Agg"')
import pandas as pd
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
df = pd.read_csv("DTree.csv")
print (df)
Age Experience Rank Nationality Go
0 36 10 9 UK NO
1 42 12 4 USA NO
2 23 4 6 N NO
3 52 4 4 USA NO
4 43 21 8 USA YES
5 44 14 5 UK NO
6 66 3 7 N YES
7 35 14 9 UK YES
8 52 13 7 N YES
9 35 5 9 N YES
10 24 3 5 USA NO
11 18 3 7 UK YES
12 45 9 9 UK YES
d = {"UK': 0, 'USA': 1, 'N': 2}
df ['Nationality'] = df ['Nationality'].map(d)
d={'YES': 1, 'NDO': 0}
df['Go'] = df['Go'] .map(d)
print (df)
Age Experience Rank Nationality
0 36 10 9 0 0
1 42 12 4 1 0
2 23 4 6 2 0
3 52 4 4 1 0
4 43 21 8 1 1
5 44 14 5 0 0
6 66 3 7 2 1
7 35 14 9 0 1
8 52 13 7 2 1
9 35 5 9 2 1
10 24 3 5 1 0
11 18 3 7 0 1
12 45 9 9 0 1
features = ['Age', 'Experience', 'Rank', 'Nationality']
y= df [features]
x=df ['Go']
X
Age Experience Rank Nationality
0 36 10 9 0
1 42 12 4 1
2 23 4 6 2
3 52 4 4 1
4 43 21 8 1
5 44 14 5 0
6 66 3 7 2
7 35 14 9 0
8 52 13 7 2
9 35 5 9 2
10 24 3 5 1
11 18 3 7 0
12 45 9 9 0
y
0 0
1 0
2 0
3 0
4 1
5 0
6 1
7 1
8 1
9 1
10 0
11 1
12 1
Name: Go, dtype: int64
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
tree.plot_tree(dtree, feature_names=features)
[Text(0.4, 0.9, 'Rank <= 6.5\ngini = 0.497\nsamples = 13\nvalue = [6, 7]'),
Text (0.2, 0.7, 'gini = 0.0\nsamples = 5\nvalue = [5, 0]'),
Text (0.6, 0.7, 'Nationality <= 0.5\ngini = 0.219\nsamples = 8\nvalue = [1,
71%),
Text (0.4, 0.5, 'Age <= 35.B5\ngini = 0.375\nsamples = 4\nvalue = [1, 3]'),
Text (0.2, 0.3, 'gini = 0.0\nsamples = 2\nvalue = [0, 2]"'),
Text (0.6, 0.3, 'Age <= 40.5\ngini = 0.5\nsamples = 2\nvalue = [1, 1]'),
Text(0.4, 0.1, 'gini = 0.0\nsamples = 1\nvalue = [1, 0]"'),
Text (0.8, 0.1, 'gini = 0.0\nsamples = 1\nvalue = [0, 1]'),
Text (0.8, 0.5, 'gini = 0.0\nsamples = 4\nvalue = [0, 4]")]
Rank == 6.5