-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
88 lines (71 loc) · 3.18 KB
/
Copy pathmodels.py
File metadata and controls
88 lines (71 loc) · 3.18 KB
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
#!/usr/bin/env python
"""
machine learning predictions, outputs accuracy and saves list of features.
input: x_train.npy, x_test.npy, y_train.npy, y_test.npy
output: predictions, top_feats.npy
"""
import os, sys
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
import numpy as np
# this script is untested, pending a working feature_selection
if __name__ =='__main__':
# x_train is the training data (words counts)
x_train = np.load('data_sets/x_train.npy')
# y_train is the pos/neg labelling of the x_train
y_train = np.load('data_sets/y_train.npy')
x_test = np.load('data_sets/x_test.npy')
y_test = np.load('data_sets/y_test.npy')
model_type = sys.argv[1]
if(model_type == 'XGB'):
# declare model
model = XGBClassifier(learning_rate=1, n_estimators=10, objective='binary:logistic', silent=True, nthreads=8)
# train model
model.fit(x_train, y_train)
# make prediction on testing data
y_pred = model.predict(x_test)
# round to nearest class (xgboost thing)
y_pred = [round(value) for value in y_pred]
# compare predictions to actual classes (y_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
# now get importance of each word
importances = model.feature_importances_
feats = np.load('data_sets/feats.npy')
for i in range(len(importances)):
top_index = np.argmax(importances)
if(importances[top_index]==0):
break
print(feats[top_index], importances[top_index])
importances[top_index] = 0
if(model_type == 'SVM'):
from sklearn import svm
model = svm.SVC()
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
if(model_type == 'MNB'):
from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB()
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))
if(model_type== 'ANN'):
from keras.layers.core import Dense, Dropout, Activation
from keras.models import Sequential
from keras.callbacks import EarlyStopping
num_feats = len(x_train[0])
model = Sequential()
early_stop = EarlyStopping(monitor='loss', patience=0, verbose=1, min_delta=0.005, mode='auto')
model.add(Dense(num_feats,activation='relu',input_dim=(num_feats)))
model.add(Dropout(0.50))
model.add(Dense(500, activation='relu', kernel_initializer='uniform'))
model.add(Dropout(0.50))
model.add(Dense(2, kernel_initializer='uniform', activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
model.fit(x_train, y_train, epochs=25, verbose=1, callbacks = [early_stop])
y_pred = model.predict_classes(x_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: %.2f%%" % (accuracy * 100.0))