-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain_and_validate.py
165 lines (154 loc) · 7 KB
/
train_and_validate.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
import gensim,numpy
import logging
import csv
import pickle
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from sklearn.naive_bayes import GaussianNB
from sklearn.neural_network import MLPClassifier
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import precision_score, \
recall_score, \
accuracy_score, f1_score
from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.structure import SoftmaxLayer
from pybrain.structure import TanhLayer
from pybrain.supervised.trainers import BackpropTrainer
import warnings
warnings.filterwarnings("ignore")
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
model = gensim.models.Word2Vec.load('model.en')
print "The model is loaded."
def vectorize(sentence):
count = 0
a = open('unknown_word').read().strip().lower()
unsolved_default_vector = model[a]
vec_list = []
for word in sentence.split():
try:
vec_list.append(model[word.lower()])
except:
count+=1
vec_list.append(unsolved_default_vector)
return sum(vec_list)/len(vec_list)
#def test_rnn(train_arrays,train_labels,test_arrays,test_labels):
# ds = SupervisedDataSet(200,1)
# for i in range(len(train_arrays)):
# a,b = train_arrays[i].tolist(),train_labels[i].tolist()
# ds.addSample(a,b)
# for i in range(len(test_arrays)):
# a,b = test_arrays[i].tolist(),test_labels[i].tolist()
# ds.addSample(a,b)
# net = buildNetwork(200, 3, 1, hiddenclass=TanhLayer, outclass=SoftmaxLayer)
# trainer = BackpropTrainer(net, ds)
# print "Training Error:"
# print trainer.train()
# print "Training Until Convergence"
# print trainer.trainUntilConvergence(maxEpochs=20,verbose=True)
# print trainer.testOnData(verbose=True)
# pickle.dump( net, open( "rnn_predictor.pickle", "wb" ) )
def test_classifiers(train_arrays,train_labels,test_arrays,test_labels):
# Logistic Regression
classifier = LogisticRegression()
classifier.fit(train_arrays, train_labels)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, penalty='l2', random_state=None, tol=0.0001)
print "Logistic Regression Classifier:"
prediction = classifier.predict(test_arrays)
print 'Accuracy:', accuracy_score(test_labels, prediction)
print 'F1 score:', f1_score(test_labels, prediction)
print 'Recall:', recall_score(test_labels, prediction)
print 'Precision:', precision_score(test_labels, prediction)
pickle.dump( classifier, open( "logistic_predictor.pickle", "wb" ) )
# Naive Bayes
classifier = GaussianNB()
classifier.fit(train_arrays, train_labels)
GaussianNB()
print "Naive Bayes Classifier:"
prediction = classifier.predict(test_arrays)
print 'Accuracy:', accuracy_score(test_labels, prediction)
print 'F1 score:', f1_score(test_labels, prediction)
print 'Recall:', recall_score(test_labels, prediction)
print 'Precision:', precision_score(test_labels, prediction)
pickle.dump( classifier, open( "nb_predictor.pickle", "wb" ) )
# Decision Trees
classifier = tree.DecisionTreeClassifier()
classifier.fit(train_arrays, train_labels)
tree.DecisionTreeClassifier()
print "Decision Tree Classifier:"
prediction = classifier.predict(test_arrays)
print 'Accuracy:', accuracy_score(test_labels, prediction)
print 'F1 score:', f1_score(test_labels, prediction)
print 'Recall:', recall_score(test_labels, prediction)
print 'Precision:', precision_score(test_labels, prediction)
pickle.dump( classifier, open( "dt_predictor.pickle", "wb" ) )
# Random Forest
classifier = RandomForestClassifier(n_estimators = 100)
classifier.fit(train_arrays, train_labels)
RandomForestClassifier()
print "Random Forest Classifier:"
prediction = classifier.predict(test_arrays)
print 'Accuracy:', accuracy_score(test_labels, prediction)
print 'F1 score:', f1_score(test_labels, prediction)
print 'Recall:', recall_score(test_labels, prediction)
print 'Precision:', precision_score(test_labels, prediction)
pickle.dump( classifier, open( "random_for_predictor.pickle", "wb" ) )
# MLP Neural Network
classifier = MLPClassifier(algorithm='l-bfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
classifier.fit(train_arrays, train_labels)
MLPClassifier(activation='relu', algorithm='l-bfgs', alpha=1e-05,
batch_size=200, beta_1=0.9, beta_2=0.999, early_stopping=False,
epsilon=1e-08, hidden_layer_sizes=(5, 2), learning_rate='constant',
learning_rate_init=0.001, max_iter=200, momentum=0.9,
nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True,
tol=0.0001, validation_fraction=0.1, verbose=False,
warm_start=False)
print "MLP Neural Network Classifier:"
prediction = classifier.predict(test_arrays)
print 'Accuracy:', accuracy_score(test_labels, prediction)
print 'F1 score:', f1_score(test_labels, prediction)
print 'Recall:', recall_score(test_labels, prediction)
print 'Precision:', precision_score(test_labels, prediction)
pickle.dump( classifier, open( "neural_net_predictor.pickle", "wb" ) )
# Support Vector Machines
classifier = svm.SVC()
classifier.fit(train_arrays, train_labels)
svm.SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
print "Support Vector Machines Classifier:"
prediction = classifier.predict(test_arrays)
print 'Accuracy:', accuracy_score(test_labels, prediction)
print 'F1 score:', f1_score(test_labels, prediction)
print 'Recall:', recall_score(test_labels, prediction)
print 'Precision:', precision_score(test_labels, prediction)
pickle.dump( classifier, open( "svm_predictor.pickle", "wb" ) )
return None
def train(corpus):
fp = open(corpus, 'rb' )
reader = csv.reader( fp, delimiter=',', quotechar='"', escapechar='\\' )
train_len,test_len = 180000,20000
train_arrays,train_labels = numpy.zeros((train_len,200)),numpy.zeros(train_len)
test_arrays,test_labels = numpy.zeros((test_len,200)),numpy.zeros(test_len)
count = 0
for row in reader:
m = 1 if int(row[0])>0 else 0
if count < train_len:
train_arrays[count] = vectorize(row[5])
train_labels[count] = m
else:
test_arrays[count-train_len] = vectorize(row[5])
test_labels[count-train_len] = m
count+=1
if count>=(train_len+test_len):
break
print "Done making Arrays"
print "Starting Testing"
print "RNN:"
# test_rnn(train_arrays,train_labels,test_arrays,test_labels)
test_classifiers(train_arrays,train_labels,test_arrays,test_labels)
if __name__ == '__main__':
train('Data/sentiment.csv')