AI_PRO/decisiontree.py
2021-05-19 05:04:00 +02:00

54 lines
1.3 KiB
Python

import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
df = pandas.read_csv("treedata\\data.csv")
#Map text values to number values
d = {'toPlow' : 0, 'toWater' : 1, 'toSeed' : 2, 'toFertilize' : 3, 'toCut' : 4}
df['Field'] = df['Field'].map(d)
d = {'Night' : 0, 'Day' : 1}
df['Day Time'] = df['Day Time'].map(d)
d = {'Clear Sky' : 0, 'Cloudy' : 1, 'Rainy' : 2, 'Hail': 3}
df['Weather'] = df['Weather'].map(d)
d = {'Freezing' : 0, 'Cold' : 1, 'Mild': 2, 'Hot': 3}
df['Temperature'] = df['Temperature'].map(d)
d = {'Windless' : 0, 'Strong Wind' : 1, 'Gale': 2}
df['Wind'] = df['Wind'].map(d)
d={'Low': 0, 'High': 1}
df['Humidy'] = df['Humidy'].map(d)
d = {'Wait' : 0, 'Make Action' : 1}
df['Decision'] = df['Decision'].map(d)
#Separate the feature columns from targert columns
features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
X = df[features]
y = df['Decision']
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')
img = pltimg.imread('mydecisiontree.png')
imgplot = plt.imshow(img)
plt.show()
#print(dtree.predict([[0, 1, 0, 0, 0, 1]]))