-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.py
107 lines (92 loc) · 3.67 KB
/
dump.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
# -- coding: utf-8 --
"""
Created on Sun Dec 15 15:42:44 2019
@author: jashj
"""
from NeuralNetwork import NeuralNetwork as nn
from KNN import KNN as knn
from DTree import dtreemain
import pandas as pd
import numpy as np
import pickle
import sys
def orient(name, filename, model_file, model):
if name=='train':
if model=='nearest' or model=='best':
train=pd.read_csv(filename,sep=' ',header=None)
filename_knn=model_file
file=open(filename_knn,'wb')
pickle.dump(train,file)
if model=='nnet':
train=pd.read_csv(filename,sep=' ',header=None)
x_train=train.drop(columns=[0,1],axis=1)
y_train=train[1]
y_train=pd.get_dummies(y_train)
y_columns=y_train.columns
x_train=x_train.to_numpy()
y_train=y_train.to_numpy()
print(x_train.shape[0], 'train samples')
a=nn(25,0.001,0.9)
(w1,w2,w3,b1,b2,b3)=a.fit(x_train,y_train)
weights={'w1':w1,
'w2':w2,
'w3':w3,
'b1':b1,
'b2':b2,
'b3':b3,
'y_columns':y_columns}
filename_nn=model_file
file=open(filename_nn,'wb')
pickle.dump(weights,file)
if model =='tree':
dtreemain(name, filename, model_file)
if name=='test':
if model=='nearest' or model == 'best':
file=open(model_file,'rb')
train=pickle.load(file)
test=pd.read_csv(filename, sep=' ',header=None)
X_test=test.drop(columns=[0,1],axis=1)
y_filenames=test[0]
y_test=test[1]
X_test=X_test.to_numpy()
y_test=y_test.to_numpy()
obj=knn(10)
ypred=obj.predict(train,X_test)
f= open("output.txt","w")
for i in range(len(X_test)):
with open('output.txt','a') as f:
f.write(str(y_filenames[i])+ ' '+str(ypred[i])+'\n')
if model=='tree' :
dtreemain(name, filename, model_file)
if model=='nnet':
test=pd.read_csv(filename,sep=' ',header=None)
x_test=test.drop(columns=[0,1],axis=1)
y_test=test[1]
y_filenames=test[0]
y_test=pd.get_dummies(y_test)
x_test=x_test.to_numpy()
y_test=y_test.to_numpy()
print(x_test.shape[0], 'test samples')
file=open(model_file,'rb')
new_weights=pickle.load(file)
w1f=new_weights['w1']
w2f=new_weights['w2']
w3f=new_weights['w3']
b1f=new_weights['b1']
b2f=new_weights['b2']
b3f=new_weights['b3']
y_columns=new_weights['y_columns']
a=nn(25,0.001,0.9)
y_test_predicted=a.predict(x_test,w1f,w2f,w3f,b1f,b2f,b3f)
zero_one=(y_test_predicted == y_test_predicted.max(axis=1)[:,None]).astype(int)
diff=(y_test == zero_one).sum(axis=1)
accuracy=np.count_nonzero(diff == y_test.shape[1])
print('accuracy ',accuracy/diff.shape[0]*100)
f= open("Output.txt","w")
for i in range(len(x_test)):
with open('Output.txt','a') as f:
f.write(str(y_filenames[i])+ ' '+str(y_columns[np.argmax(y_test_predicted[i])])+'\n')
if __name__== "__main__":
if(len(sys.argv) != 5):
raise Exception('Error: expected 4 command line arguments')
orient(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])