-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify.py
210 lines (162 loc) · 5.57 KB
/
classify.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import csv
import os
import random
import numpy as np
import sys
from sklearn import svm
from keras.models import Sequential, model_from_yaml
from keras.layers import Dropout, Dense
from keras.callbacks import EarlyStopping
def open_csv(file_path):
# Input read as f_wh, f_wmt, f_posh, f_posmt, f_len, y
assert os.path.isfile(file_path)
raw_data = []
with open(file_path, 'r') as fid:
csv_reader = csv.reader(fid)
for row in csv_reader:
raw_data.append(row)
raw_data = raw_data[1:]
#random.shuffle(raw_data)
raw_data = np.array(raw_data).astype('float32')
features = raw_data[:, :-1]
tags = raw_data[:, -1].astype('int32')
return features, tags
def normalize(a):
mean = a.mean(1, keepdims=True)
std = a.std(1, keepdims=True)
b = np.subtract(a, mean)
c = np.divide(b, std)
return c
def evaluate_model(tags, predictions):
t_p = 0
t_n = 0
f_p = 0
f_n = 0
for idx in range(len(tags)):
# print("Tags: {}, Pred: {}".format(tags[idx], predictions[idx]))
if(tags[idx] == 1 and predictions[idx] == 1):
t_p += 1
elif(tags[idx] == 0 and predictions[idx] == 0):
t_n += 1
elif(tags[idx] == 0 and predictions[idx] == 1):
f_p += 1
else:
f_n += 1
precision = 0
if (t_p + f_p) > 0:
precision = float(t_p)/(t_p + f_p)
accuracy = 0
if (t_p + f_p + t_n + f_n) > 0:
accuracy = float((t_p + t_n))/(t_p + t_n + f_p + f_n)
recall = 0
if (t_p + f_n) > 0:
recall = float(t_p)/(t_p + f_n)
print("Precision: {}".format(precision))
print("Accuracy: {}".format(accuracy))
print("Recall: {}".format(recall))
def evaluate_svm_model(tags, predictions):
t_p = 0
t_n = 0
f_p = 0
f_n = 0
for idx in range(len(tags)):
# print("Tags: {}, Pred: {}".format(tags[idx], predictions[idx]))
if(tags[idx] == 1 and predictions[idx] == 1):
t_p += 1
elif(tags[idx] == 0 and predictions[idx] == 0):
t_n += 1
elif(tags[idx] == 0 and predictions[idx] == 1):
f_p += 1
else:
f_n += 1
precision = 0.
if (t_p + f_p) > 0:
precision = float(t_p)/(t_p + f_p)
accuracy = 0.
if (t_p + f_p + t_n + f_n) > 0:
accuracy = float((t_p + t_n))/(t_p + t_n + f_p + f_n)
recall = 0.
if (t_p + f_n) > 0:
recall = float(t_p)/(t_p + f_n)
print("Precision: {}".format(precision))
print("Accuracy: {}".format(accuracy))
print("Recall: {}".format(recall))
# CLASSIFIERS
def svm_classifier(X, y):
# Input Format should be X : [n_samples, n_features] , y : [n_samples, 1]
classifier = svm.SVC(verbose=1)
classifier.fit(X, y)
from sklearn.externals import joblib
joblib.dump(classifier, 'models/svm-model.pkl')
def mlp_classifier(X, y, val=None, n_epochs=20, bsize=5):
'''
:param X: numpy array [n_samples, n_features] (input features)
:param y: numpy array [n_samples, 1] (tags)
:param val: tuple of two numpy arrays (X, y) corrreponding to the validation data
:return:
'''
# Uses Keras Library (see keras.io for more details)
# Input Format should be X : [n_samples, n_features] , y : [n_samples, 1]
# ATTENTION: Inputs should be normalized for better results!!!
model = Sequential()
model.add(Dense(50, input_dim=X.shape[1], activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(50, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop')
if val is None:
model.fit(X, y, nb_epoch=n_epochs, batch_size=bsize, verbose=1,
validation_split=0.2, shuffle=True, callbacks=[EarlyStopping(patience=5)])
else:
assert isinstance(val, tuple)
model.fit(X, y, nb_epoch=n_epochs, batch_size=bsize, verbose=1,
validation_data=val, shuffle=True, callbacks=[EarlyStopping(patience=5)])
yaml_string = model.to_yaml()
open('models/mlp_architecture.yaml', 'w').write(yaml_string)
model.save_weights('models/mlp_model_weights.h5', overwrite=True)
def mlp_predict(X, bsize=5):
'''
:param X: numpy array [n_samples, n_features] (input features)
:param model: path to yaml file containing model
:param weights: path to h5 file containing model weights
:return: prediction: numpy array with predictions
'''
model = model_from_yaml(open('models/mlp_architecture.yaml').read())
model.load_weights('models/mlp_model_weights.h5')
predictions = model.predict(X, batch_size=bsize, verbose=1)
return predictions
def svm_predict(X):
from sklearn.externals import joblib
classifier = joblib.load('models/svm-model.pkl')
predictions = classifier.predict(X)
return predictions
#path = sys.argv[1]
path = "features/features_train.csv"
features, tags = open_csv(path)
features = normalize(features)
print "Building Deep NN Classifier Model"
mlp_classifier(features, tags, bsize=50)
print "Predicting with Deep NN Classifier"
predictions = mlp_predict(features)
print predictions
evaluate_model(tags, predictions)
'''
from matplotlib import pyplot
T=[]
F=[]
for i in tags:
if tags[i] == 1:
T.append(features[i, :])
else:
F.append(features[i, :])
T = np.asarray(T)
F = np.asarray(F)
pyplot.plot()
'''
print "Building SVM Model"
svm_classifier(features, tags)
print("Predicting with svm")
predictions = svm_predict(features)
print(predictions)
evaluate_svm_model(tags, predictions)