decision tree update
This commit is contained in:
parent
3b44694641
commit
e10fbe4e48
4
Field.py
4
Field.py
@ -25,9 +25,9 @@ class Field:
|
|||||||
elif self.state == 'toFertilize':
|
elif self.state == 'toFertilize':
|
||||||
return 3
|
return 3
|
||||||
elif self.state == 'toWater':
|
elif self.state == 'toWater':
|
||||||
return 200
|
return 4
|
||||||
elif self.state == 'toCut':
|
elif self.state == 'toCut':
|
||||||
return 99
|
return 100
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
283
Testfile.py
283
Testfile.py
@ -1,221 +1,122 @@
|
|||||||
from itertools import product
|
from itertools import product
|
||||||
import numpy as np
|
|
||||||
import pandas as pd
|
|
||||||
import csv
|
import csv
|
||||||
import id3test
|
|
||||||
import pprint
|
# stan pola: toCut, toPlow, toWater, toSeed, toFertilize
|
||||||
import sys
|
# pora dnia: dzień, noc
|
||||||
import pandas
|
# pogoda: sunny, cloudy, rainy, hail
|
||||||
from sklearn import tree
|
# temperatura: freezing, cold, mild, hot
|
||||||
import pydotplus
|
# wiatr: windless, strong wind, gale
|
||||||
from sklearn.tree import DecisionTreeClassifier
|
# humidy: low, high
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
import matplotlib.image as pltimg
|
|
||||||
|
|
||||||
field_states = ['toPlow', 'toWater', 'toSeed', 'toFertilize', 'toCut']
|
field_states = ['toPlow', 'toWater', 'toSeed', 'toFertilize', 'toCut']
|
||||||
header = ['Equipped', 'Not Equipped']
|
day_time = ['Day', 'Night']
|
||||||
hitch = ['Tillage Unit', 'Crop Trailer']
|
weather = ['Clear Sky', 'Cloudy', 'Rainy', 'Hail']
|
||||||
tillage_unit = ['Seeds', 'Water', 'Fertilizer', 'Nothing']
|
temperature = ['Freezing', 'Cold', 'Mild', 'Hot']
|
||||||
engine_working = ['Yes', 'No']
|
wind = ['Windless', 'Strong Wind', 'Gale']
|
||||||
in_base = ['Yes', 'No']
|
humidy = ['Low', 'High']
|
||||||
fuel = ['Enough', 'Not_Enough']
|
|
||||||
|
|
||||||
# output = list(product(field_states, header, hitch, tillage_unit, engine_working, in_base, fuel))
|
output = list(product(field_states, day_time, weather, temperature, wind, humidy))
|
||||||
output = list(product(field_states, header, hitch, tillage_unit, in_base))
|
|
||||||
|
|
||||||
dict = []
|
dict = []
|
||||||
# for x in range(len(output)):
|
|
||||||
# # dict.append({'field': output[x][0], 'header' : output[x][1], 'hitch' : output[x][2],
|
|
||||||
# # 'tillage_unit' : output[x][3], 'engine_working' : output[x][4],
|
|
||||||
# # 'in_base' : output[x][5], 'fuel': output[x][6]})
|
|
||||||
# print(x+1, output[x])
|
|
||||||
|
|
||||||
decisions = []
|
|
||||||
curr_decision = "Make Action"
|
|
||||||
|
|
||||||
for x in range(len(output)):
|
for x in range(len(output)):
|
||||||
aField = output[x][0]
|
if x % 3 == 0:
|
||||||
aHeader = output[x][1]
|
|
||||||
aHitch = output[x][2]
|
|
||||||
aTillage_unit = output[x][3]
|
|
||||||
aIn_base = output[x][4]
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
curr_decision = "Make Action"
|
mField = output[x][0]
|
||||||
|
mDay_time = output[x][1]
|
||||||
|
mWeather = output[x][2]
|
||||||
|
mTemperature = output[x][3]
|
||||||
|
mWind = output[x][4]
|
||||||
|
mHumidy = output[x][5]
|
||||||
|
mDecision = 'null'
|
||||||
|
|
||||||
if aField == 'toCut':
|
# pora dnia: dzień 2, noc -2
|
||||||
if not aHeader == 'Equipped':
|
# pogoda: sunny+3, cloudy+3, rainy-2, hail-5
|
||||||
if aIn_base == 'Yes':
|
# temperatura: freezing -3, cold-1, mild+4, hot+2
|
||||||
curr_decision = 'Change Header'
|
# wiatr: windless +2, strong wind-1, gale-3
|
||||||
|
# humidy: low+2, high-3
|
||||||
|
|
||||||
|
if mDay_time == 'Day':
|
||||||
|
valDay_time = 2
|
||||||
|
else:
|
||||||
|
valDay_time = -3
|
||||||
|
|
||||||
|
if mWeather == 'Clear Sky' or 'Cloudy':
|
||||||
|
valWeather = 3
|
||||||
|
elif mWeather == 'Rainy':
|
||||||
|
valWeather = -2
|
||||||
|
else:
|
||||||
|
valWeather = -5
|
||||||
|
|
||||||
|
if mTemperature == 'Freezing':
|
||||||
|
valTemperature = -3
|
||||||
|
elif mTemperature == 'Cold':
|
||||||
|
valTemperature = -1
|
||||||
|
elif mTemperature == 'Mild':
|
||||||
|
valTemperature = 4
|
||||||
|
else:
|
||||||
|
valTemperature = 2
|
||||||
|
|
||||||
|
if mWind == 'Windless':
|
||||||
|
valWind = +2
|
||||||
|
elif mWind == 'Strong Wind':
|
||||||
|
valWind = -1
|
||||||
|
else:
|
||||||
|
valWind = -3
|
||||||
|
|
||||||
|
if humidy == 'Low':
|
||||||
|
valHumidy = 2
|
||||||
|
else:
|
||||||
|
valHumidy = -2
|
||||||
|
|
||||||
|
result = valDay_time + valWeather + valTemperature + valWind + valHumidy
|
||||||
|
if result >= 0:
|
||||||
|
mDecision = "Make Action"
|
||||||
|
else:
|
||||||
|
mDecision = "Wait"
|
||||||
|
|
||||||
|
#Special conditions
|
||||||
|
if mDay_time == 'Night' and (mField == 'toWater' or mField == 'toCut'):
|
||||||
|
mDecision = "Make Action"
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
curr_decision = 'Go To Base'
|
mDecision = "Wait"
|
||||||
|
|
||||||
|
if mWeather == 'Rainy' and (mField == 'toPlow' or mField == 'toSeed'):
|
||||||
|
mDecision = "Make Action"
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
mDecision = 'Wait'
|
||||||
|
|
||||||
|
if mWeather == 'Hail':
|
||||||
|
mDecision = 'Wait'
|
||||||
break
|
break
|
||||||
|
|
||||||
if not aHitch == 'Crop Trailer':
|
if mTemperature == 'Freezing':
|
||||||
if aIn_base == 'Yes':
|
mDecision = 'Wait'
|
||||||
curr_decision = 'Change Hitch'
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
break
|
||||||
|
|
||||||
if aField == 'toPlow':
|
if mWind == 'Gale':
|
||||||
if aHeader == 'Equipped':
|
mDecision = 'Wait'
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = 'Change Header'
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
break
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
if mHumidy == 'High' and mField == 'toCut':
|
||||||
if aIn_base == "Yes":
|
mDecision = "Wait"
|
||||||
curr_decision = "Change Hitch"
|
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
curr_decision = "Go To Base"
|
mDecision = "Make Action"
|
||||||
break
|
|
||||||
if not aTillage_unit == "Nothing":
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Load"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
break
|
||||||
|
|
||||||
if aField == 'toSeed':
|
dict.append({'Field': mField, 'Day Time': mDay_time, 'Weather': mWeather,
|
||||||
if aHeader == 'Equipped':
|
'Temperature': mTemperature, 'Wind': mWind, 'Humidy': mHumidy, 'Decision': mDecision})
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = 'Change Header'
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
fields = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy', 'Decision']
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Hitch"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
|
||||||
if not aTillage_unit == "Seeds":
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Load"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
|
||||||
|
|
||||||
if aField == 'toWater':
|
filename = "treedata\\data.csv"
|
||||||
if aHeader == 'Equipped':
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = 'Change Header'
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Hitch"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
|
||||||
if not aTillage_unit == "Water":
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Load"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
|
||||||
|
|
||||||
if aField == 'toFertilize':
|
|
||||||
if aHeader == 'Equipped':
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = 'Change Header'
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Hitch"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
|
||||||
if not aTillage_unit == "Fertilizer":
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Load"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
|
||||||
if aIn_base == 'Yes':
|
|
||||||
curr_decision = 'Go To Field'
|
|
||||||
break
|
|
||||||
dict.append({'Field': aField, 'Header': aHeader, 'Hitch': aHitch, 'Tillage_Unit': aTillage_unit,
|
|
||||||
'In_Base': aIn_base, 'Decision': curr_decision})
|
|
||||||
|
|
||||||
print(dict)
|
|
||||||
|
|
||||||
fields = ['Field', 'Header', 'Hitch', 'Tillage_Unit', 'In_Base', 'Decision']
|
|
||||||
|
|
||||||
filename = "treedata\\data2.csv"
|
|
||||||
|
|
||||||
with open(filename, 'w') as csvfile:
|
with open(filename, 'w') as csvfile:
|
||||||
writer = csv.DictWriter(csvfile, fieldnames=fields)
|
writer = csv.DictWriter(csvfile, fieldnames=fields)
|
||||||
writer.writeheader()
|
writer.writeheader()
|
||||||
writer.writerows(dict)
|
writer.writerows(dict)
|
||||||
|
|
||||||
df = pandas.read_csv("treedata\\data2.csv")
|
|
||||||
print(df)
|
|
||||||
|
|
||||||
# Map text values to number values
|
|
||||||
d = {'toPlow': 0, 'toWater': 1, 'toSeed': 2, 'toFertilize': 3, 'toCut': 4}
|
|
||||||
df['Field'] = df['Field'].map(d)
|
|
||||||
|
|
||||||
d = {'Not Equipped': 0, 'Equipped': 1}
|
|
||||||
df['Header'] = df['Header'].map(d)
|
|
||||||
|
|
||||||
d = {'Tillage Unit': 0, 'Crop Trailer': 1}
|
|
||||||
df['Hitch'] = df['Hitch'].map(d)
|
|
||||||
|
|
||||||
d = {'Nothing': 0, 'Seeds': 1, 'Water': 2, 'Fertilizer': 3}
|
|
||||||
df['Tillage_Unit'] = df['Tillage_Unit'].map(d)
|
|
||||||
|
|
||||||
d = {'No': 0, 'Yes': 1}
|
|
||||||
df['In_Base'] = df['In_Base'].map(d)
|
|
||||||
|
|
||||||
d = {'Make Action': 0, 'Change Header': 1, 'Go To Base': 2, 'Change Hitch': 3, 'Change Load': 4, 'Go To Field': 5}
|
|
||||||
df['Decision'] = df['Decision'].map(d)
|
|
||||||
|
|
||||||
# Separate the feature columns from targert columns
|
|
||||||
|
|
||||||
features = ['Field', 'Header', 'Hitch', 'Tillage_Unit', 'In_Base']
|
|
||||||
|
|
||||||
X = df[features]
|
|
||||||
y = df['Decision']
|
|
||||||
|
|
||||||
# FIELD 'toPlow' : 0, 'toWater' : 1, 'toSeed' : 2, 'toFertilize' : 3, 'toCut' : 4
|
|
||||||
# HEADER 'Not Equipped' : 0, 'Equipped' : 1
|
|
||||||
# HITCH 'Tillage Unit' : 0, 'Crop Trailer' : 1
|
|
||||||
# TILLAGE 'Nothing' : 0, 'Seeds' : 1, 'Water': 2, 'Fertilizer': 3
|
|
||||||
# IN BASE 'No' : 0, 'Yes' : 1
|
|
||||||
|
|
||||||
|
|
||||||
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, 1, 3, 0]]))
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
def checkConditions():
|
def checkConditions():
|
||||||
weather = random.choice(['Sunny', 'Cloudy', 'Rainy', 'Hail'])
|
weather = random.choice(['Clear Sky', 'Cloudy', 'Rainy', 'Hail'])
|
||||||
day_time = random.choice(['Day', 'Night'])
|
day_time = random.choice(['Day', 'Night'])
|
||||||
temperature = random.choice(['Freezing', 'Cold', 'Mild', 'Hot'])
|
temperature = random.choice(['Freezing', 'Cold', 'Mild', 'Hot'])
|
||||||
wind = random.choice(['Windless', 'Strong Wind', 'Gale'])
|
wind = random.choice(['Windless', 'Strong Wind', 'Gale'])
|
||||||
|
@ -23,13 +23,12 @@ df['Temperature'] = df['Temperature'].map(d)
|
|||||||
d = {'Windless' : 0, 'Strong Wind' : 1, 'Gale': 2}
|
d = {'Windless' : 0, 'Strong Wind' : 1, 'Gale': 2}
|
||||||
df['Wind'] = df['Wind'].map(d)
|
df['Wind'] = df['Wind'].map(d)
|
||||||
|
|
||||||
d={'Low': 0, 'High': 1}
|
d = {'Low': 0, 'High': 1}
|
||||||
df['Humidy'] = df['Humidy'].map(d)
|
df['Humidy'] = df['Humidy'].map(d)
|
||||||
|
|
||||||
d = {'Wait' : 0, 'Make Action' : 1}
|
d = {'Wait' : 0, 'Make Action' : 1}
|
||||||
df['Decision'] = df['Decision'].map(d)
|
df['Decision'] = df['Decision'].map(d)
|
||||||
|
|
||||||
|
|
||||||
#Separate the feature columns from targert columns
|
#Separate the feature columns from targert columns
|
||||||
|
|
||||||
features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
|
features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
|
||||||
@ -37,7 +36,6 @@ features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
|
|||||||
X = df[features]
|
X = df[features]
|
||||||
y = df['Decision']
|
y = df['Decision']
|
||||||
|
|
||||||
|
|
||||||
dtree = DecisionTreeClassifier()
|
dtree = DecisionTreeClassifier()
|
||||||
dtree = dtree.fit(X, y)
|
dtree = dtree.fit(X, y)
|
||||||
|
|
||||||
|
BIN
mydecisiontree.png
Normal file
BIN
mydecisiontree.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 122 KiB |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user