Skip to content

Instantly share code, notes, and snippets.

@bis-carbon
Last active December 22, 2016 05:13
Show Gist options
  • Save bis-carbon/cfc8976e6d8b7b62aa58050f5ad682c5 to your computer and use it in GitHub Desktop.
Save bis-carbon/cfc8976e6d8b7b62aa58050f5ad682c5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
import operator
import os
import math
import sys
import collections
def main():
with open('result.txt', 'w') as file:
file.writelines("")
data_set_dict()
def read_tree_dict():
with open("tree.txt") as textFile:
data=textFile.read()
decision_tree = eval(data)
return decision_tree
def data_set_dict(): #read data set file in to list of dictionaries
data = {}
tree = read_tree_dict()
lines = update_null(read_csv())
features = lines[0]
add_decision1(features, "Decision")
for x in lines[1:]:
i=0
for y in x:
data.update({features[i]:y})
i+=1
decision = make_decision(tree,data)
data={}
add_decision(x, decision)
def update_null(data1):
i=0
for values3 in data1:
j=0
for x in values3:
if not x:
values3[j]='0'
j+=1
data1[i]=values3
i+=1
return data1
def add_decision1(data, decision):
data.append(decision)
print (data)
write_file(data)
def add_decision(data, decision):
if not check_value(decision):
data.append(decision)
else:
data.append(list(decision.values())[0])
print (data)
write_file(data)
def read_csv():
list1 = []
fh = open('delaytest.csv', 'r')
for line in fh:
list1.append(line.strip().split(','))
return list1
def write_file(output):
with open('result.txt', 'a') as file:
file.writelines(" ".join(output))
file.writelines('\n')
def make_decision(decision_tree, data_s):
dict_tree = decision_tree
while (check_value(dict_tree)):
node =list(dict_tree.keys())[0]
dict_tree = dict_tree[node]
value = data_s[node]
if not value in dict_tree:
break
else:
dict_tree = dict_tree[value]
return dict_tree
def check_value(dic_value): #check if value of a dictionary is null if so then the key is the decision value
if isinstance(dic_value,dict):
return True
return False
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment