Run ❯
Get your
own Python
server
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python code
data.csv
#Three lines to make our compiler able to draw: import sys import matplotlib matplotlib.use('Agg') import pandas from sklearn import tree from sklearn.tree import DecisionTreeClassifier import matplotlib.pyplot as plt df = pandas.read_csv("data.csv") d = {'UK': 0, 'USA': 1, 'N': 2} df['Nationality'] = df['Nationality'].map(d) d = {'YES': 1, 'NO': 0} df['Go'] = df['Go'].map(d) features = ['Age', 'Experience', 'Rank', 'Nationality'] X = df[features] y = df['Go'] dtree = DecisionTreeClassifier() dtree = dtree.fit(X, y) tree.plot_tree(dtree, feature_names=features) #Two lines to make our compiler able to draw: plt.savefig(sys.stdout.buffer) sys.stdout.flush() #NOTE: #You will see that the Decision Tree gives you different results if you run it enough times, even if you feed it with the same data. #That is because the Decision Tree does not give us a 100% certain answer. It is based on the probability of an outcome, and the answer will vary.
Age,Experience,Rank,Nationality,Go 36,10,9,UK,NO 42,12,4,USA,NO 23,4,6,N,NO 52,4,4,USA,NO 43,21,8,USA,YES 44,14,5,UK,NO 66,3,7,N,YES 35,14,9,UK,YES 52,13,7,N,YES 35,5,9,N,YES 24,3,5,USA,NO 18,3,7,UK,YES 45,9,9,UK,YES