-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorient.py
133 lines (102 loc) · 4.22 KB
/
orient.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 2 23:28:28 2018
@author: nithish k
I have written adaboost, knn , forest in different modules and calling them here.
Please check,
knn.py, forest.py , adaboost.py for their respective code
Please refer the report in the pdf commited in the git hub
"""
from forest import forest
from adaboost import AdaBoost
from boostedforest import boostedForest
import knn
import pandas as pd
import numpy as np
import itertools as itr
import random
import collections as col
import math
import tqdm
import sys
import os
import pickle as pk
if len(sys.argv) < 5:
print("Usage: \n./orient.py train train_file.txt model_file.txt [model]")
sys.exit()
#train train_file.txt model_file.txt [model]
#nearest, adaboost, forest, or best.
(trainOrTest, train_test_file, model_file, model) = sys.argv[1:5]
#train train-data.txt forest_model.txt forest
#test test-data.txt forest_model.txt forest
if model == 'forest' :
if trainOrTest == 'train':
myForest = forest(maxDepth=5,numTrees= 20,verbose = False)
TrainX,TrainY,TrainXID= myForest.getDataFromFile(train_test_file)
myForest.trainForest(TrainX,TrainY)
pk.dump(myForest,open(model_file,'wb'))
if trainOrTest == 'test':
try:
myForest = pk.load(open(model_file,'rb'))
except:
print("output file has not been generated")
if myForest.isTrained:
Xtest,yTest,XtestID = myForest.getDataFromFile(train_test_file)
finalPredictions = myForest.predict(Xtest)
myForest.writeToFile(XtestID,finalPredictions,'output_forest.txt')
myForest.writeToFile(XtestID,finalPredictions,'output.txt')
print("Accuracy is: " ,100*sum(finalPredictions==yTest)/len(yTest))
else:
print("Untrained model being tested")
#train train-data.txt adaboost_model.txt adaboost
#test test-data.txt adaboost_model.txt adaboost
elif model == 'adaboost' :
if trainOrTest == 'train':
myBoost = AdaBoost(200,verbose = False)
TrainX,TrainY,TrainXID = myBoost.getDataFromFile(train_test_file)
myBoost.train(TrainX,TrainY)
pk.dump(myBoost,open(model_file,'wb'))
if trainOrTest == 'test':
try:
myBoost = pk.load(open(model_file,'rb'))
except:
print("output file has not been generated")
if myBoost.isTrained:
Xtest,yTest,XtestID = myBoost.getDataFromFile(train_test_file)
finalPredictions = myBoost.predict(Xtest)
myBoost.writeToFile(XtestID,finalPredictions,'output_adaboost.txt')
myBoost.writeToFile(XtestID,finalPredictions,'output.txt')
print("Accuracy is: " ,100*sum(finalPredictions==yTest)/len(yTest))
else:
print("Untrained model being tested")
#train train-data.txt nearest_model.txt nearest
#test test-data.txt nearest_model.txt nearest
elif model == 'nearest' :
if trainOrTest == 'train':
knn.train(train_test_file,model_file)
if trainOrTest == 'test':
try:
myKnn = open(model_file,'rb')
except:
print("output file has not been generated")
finalPredictions,yTest,XtestID= knn.test(48,model_file ,train_test_file)
knn.writeToFile(XtestID,finalPredictions,'output_nearest.txt')
knn.writeToFile(XtestID,finalPredictions,'output.txt')
print("Accuracy is: ",knn.accuracy(finalPredictions,yTest))
#train train-data.txt best_model.txt best
#test test-data.txt best_model.txt best
elif model == 'best' :
if trainOrTest == 'train':
knn.train(train_test_file,model_file)
if trainOrTest == 'test':
try:
myKnn = open(model_file,'rb')
except:
print("output file has not been generated")
finalPredictions,yTest,XtestID= knn.test(48,model_file ,train_test_file)
knn.writeToFile(XtestID,finalPredictions,'output_best.txt')
knn.writeToFile(XtestID,finalPredictions,'output.txt')
print("Accuracy is: " ,knn.accuracy(finalPredictions,yTest))
else:
print("Unknown model please check the model name ")