-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
40 lines (34 loc) · 1.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#
import decisiontree as dt
import randomforest as rf
import csv
def main(csvfile):
with open(csvfile, 'rb') as file:
reader = csv.reader(file)
dataset = list(reader)
all_data = dataset[1:]
featrues = dataset[0][:-1]
#bags = rf.divbootstrap(all_data, 2)
#print bags
split = int (0.8*len(all_data))
trainingset = all_data[:split]
testset = all_data[split:]
#sub_data, feat = rf.subfeatures(trainingset)
#print feat
test_value = []
test_label = []
for test in testset:
test_value.append(test[:-1])
test_label.append(test[-1])
tennisTrees = rf.buildtrees(trainingset,featrues,20)
#prec = random_predict(tennisTrees, test_value,tennisfeatrues)
#print prec
a = rf.random_accuracy(tennisTrees,test_value,featrues,test_label)
print 'The accuracy is ', 100*a, '% .'
print "random forest: "
print "For tnnis dataset:"
main('datafile/tennis.csv')
print "For banks dataset:"
main('datafile/banks.csv')
print "For politics dataset:"
main('datafile/politics.csv')