2021-05-19 04:02:14 +02:00
|
|
|
import pandas
|
|
|
|
from sklearn import tree
|
|
|
|
import pydotplus
|
|
|
|
from sklearn.tree import DecisionTreeClassifier
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import matplotlib.image as pltimg
|
2021-05-31 21:05:42 +02:00
|
|
|
import joblib
|
2021-05-19 04:02:14 +02:00
|
|
|
|
2021-05-19 04:11:26 +02:00
|
|
|
df = pandas.read_csv("treedata\\data.csv")
|
2021-05-19 04:02:14 +02:00
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2021-05-19 04:45:37 +02:00
|
|
|
d = {'Clear Sky' : 0, 'Cloudy' : 1, 'Rainy' : 2, 'Hail': 3}
|
2021-05-19 04:02:14 +02:00
|
|
|
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)
|
|
|
|
|
2021-05-19 15:04:10 +02:00
|
|
|
d = {'Low': 0, 'High': 1}
|
2021-05-19 04:02:14 +02:00
|
|
|
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)
|
2021-05-31 21:05:42 +02:00
|
|
|
joblib.dump(dtree, 'treedata\\DecisionTree.pkl')
|
|
|
|
|
2021-05-19 04:02:14 +02:00
|
|
|
|
2021-05-19 05:04:00 +02:00
|
|
|
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
|
|
|
|
graph = pydotplus.graph_from_dot_data(data)
|
2021-05-31 21:05:42 +02:00
|
|
|
graph.write_png('treedata\\mydecisiontree.png')
|
2021-05-19 05:04:00 +02:00
|
|
|
|
2021-05-31 21:05:42 +02:00
|
|
|
img = pltimg.imread('treedata\\mydecisiontree.png')
|
2021-05-19 05:04:00 +02:00
|
|
|
imgplot = plt.imshow(img)
|
|
|
|
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-19 04:02:14 +02:00
|
|
|
#print(dtree.predict([[0, 1, 0, 0, 0, 1]]))
|