# Three lines to make Python 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 )
# Predict new values.
# Should I go see a show
# starring a 40 years old American comedian,
# with 10 years of experience, and
# a comedy ranking of 6?
print( dtree.predict( [[40, 10, 6, 1]] ) )
print( "[1] means 'GO'" )
print( "[0] means 'NO'" )
tree.plot_tree( dtree, feature_names=features )
# Two lines to make our Python able to draw:
plt.savefig( sys.stdout.buffer )
sys.stdout.flush( )
|
|