-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning.py
More file actions
168 lines (134 loc) · 3.85 KB
/
learning.py
File metadata and controls
168 lines (134 loc) · 3.85 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
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
#
# python run.py train.txt test.txt
#
# The train.txt and test.txt must be of the following format:
# 0 1 0 0 ... 0 0 1 0 : 0
import sys
import glob
import os
import re
import time
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.lda import LDA
from sklearn.qda import QDA
from sklearn.linear_model import SGDRegressor
from sklearn import linear_model
#
# Checking command line arguments
#
f_train = ""
f_test = ""
if len(sys.argv) == 3:
f_train = sys.argv[1]
f_test = sys.argv[2]
else:
print "Usage: python learning.py <train-data> <test-data>"
quit()
#
# Training Phase
#
features = []
solutions = []
traindata = []
num_dups = 0
num_data = 0
num_cflct = 0
# read without duplications
with open(f_train) as f:
for line in f:
data = re.split(r':',line)
proved = float(data[1].strip())
wv = data[0]
wv = re.split(r' |\t|\n', wv)
wv = filter(lambda x:x.strip() != '', wv)
wv = map(lambda x:int(x),wv)
if (wv,proved) not in traindata:
traindata.append((wv,proved))
else:
num_dups = num_dups + 1
num_data = num_data + 1
print "Data without duplicates:"
print ""
for i in range(len(traindata)):
(f,s) = traindata[i]
print s, f
features.append(f)
solutions.append(s)
print ""
print "#data : %d" % num_data
print "#duplucates : %d" % num_dups
print "#final-training-data : %d" % (len(traindata))
#
# Read test data
#
testFeatures = []
testSolutions = []
with open(f_test) as f:
for line in f:
data = re.split(r':',line)
proved = float(data[1].strip())
wv = data[0]
wv = re.split(r' |\t|\n', wv)
wv = filter(lambda x:x.strip() != '', wv)
wv = map(lambda x:int(x),wv)
testFeatures.append (wv)
testSolutions.append(proved)
#
# prediction
#
C = 1.0
classifiers = [('Nearest Neighbors', KNeighborsClassifier(5)),
('Logistic Regression (l1)', LogisticRegression(C=C, penalty='l1')),
('Logistic Regression (l2)', LogisticRegression(C=C, penalty='l2')),
('Linear SVM', SVC(kernel='linear',C=C,probability=True,random_state=0)),
('RBF SVM', SVC(gamma=2,C=C,probability=True,random_state=0)),
('Decision Tree', DecisionTreeClassifier(max_depth=10)),
('Random Forest', RandomForestClassifier(max_depth=10,n_estimators=10,max_features=1)),
# ('AdaBoost', AdaBoostClassifier()),
('Naive Bayes', GaussianNB())]
index = 0;
for (name, clf) in classifiers:
start_time = time.time()
index = index + 1
clf.fit (features,solutions)
print ""
print "%d" % index,
print ".", name
print clf
correct = 0
incorrect = 0
true_total = 0
false_total = 0
true_hits = 0
false_hits = 0
for i in range(len(testFeatures)):
pred = clf.predict(testFeatures[i])[0]
solution = testSolutions[i]
if pred == solution:
correct = correct + 1
else:
incorrect = incorrect + 1
if solution == 1:
true_total = true_total + 1
if pred == 1:
true_hits = true_hits + 1
if solution == 0:
false_total = false_total + 1
if pred == 0:
false_hits = false_hits + 1
total = correct + incorrect
print "True hits : %d / %d (%d%%)" % (true_hits, true_total, true_hits * 100 / true_total)
print "False hits : %d / %d (%d%%)" % (false_hits, false_total, false_hits * 100 / false_total)
print "Total : %d / %d (%d%%)" % (correct,total,correct * 100 / total)
end_time = time.time()
print "%.2f" % (end_time - start_time),
print "sec"
# if hasattr(clf, "feature_importances_"):
# importances = clf.feature_importances_
# print importances