-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizations.py
189 lines (149 loc) · 6.05 KB
/
visualizations.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
import numpy
from sklearn.ensemble import RandomForestClassifier
from weight_evolution import EvolModel
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import LogisticRegression
import pylab
import collections
import math
import datetime
def shortest_paths_misclassified(train_features_file, train_example_file, test_features_file, test_example_file, etype):
rf = RandomForestClassifier(n_estimators=100)
#rf = MLPClassifier(hidden_layer_sizes = (100, 50, 50))
train_examples = numpy.load(train_features_file)
train_zip = numpy.load(train_example_file)
train_labels = [tup[1] for tup in train_zip]
test_examples = numpy.load(test_features_file)
test_zip = numpy.load(test_example_file)
test_labels = [tup[1] for tup in test_zip]
print 'Training model', rf
rf.fit(train_examples, train_labels)
preds = rf.predict(test_examples)
print "Getting Shortest Paths"
short_paths = []
short_paths_false_pos = []
short_paths_false_neg = []
for i in range(preds.size):
if preds[i] == -1 and test_labels[i] == 1:
dist = abs(train_examples[i][0])
if dist == 1: dist = 11.0
short_paths.append(dist)
short_paths_false_neg.append(dist)
elif preds[i] == 1 and test_labels[i] == -1:
dist = abs(train_examples[i][0])
if dist == 1: dist = 11.0
short_paths.append(dist)
short_paths_false_pos.append(dist)
counts = collections.Counter(short_paths)
counts_neg = collections.Counter(short_paths_false_neg)
counts_pos = collections.Counter(short_paths_false_pos)
print counts, counts_neg, counts_pos
x = []
y = []
y_neg = []
y_pos = []
for i in range(1, 12):
x.append(i)
if i in counts:
y.append(counts[float(i)])
else:
y.append(0)
if i in counts_neg:
y_neg.append(counts_neg[float(i)])
else:
y_neg.append(0)
if i in counts_pos:
y_pos.append(counts_pos[float(i)])
else:
y_pos.append(0)
labels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'No Path']
f = pylab.figure()
ax = f.add_axes([0.1, 0.1, 0.8, 0.8])
ax.bar(x, y, align='center')
ax.set_yscale('log')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_xlabel('Shortest Path')
ax.set_ylabel('Number of Edges')
ax.set_title('Shortest Paths Between Misclassified ' +etype+ ' Edges')
f.show()
f = pylab.figure()
ax = f.add_axes([0.1, 0.1, 0.8, 0.8])
ax.bar(x, y_neg, align='center')
ax.set_yscale('log')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_xlabel('Shortest Path')
ax.set_ylabel('Number of Edges')
ax.set_title('Shortest Paths Between False Negative ' +etype+ ' Edges')
f.show()
f = pylab.figure()
ax = f.add_axes([0.1, 0.1, 0.8, 0.8])
ax.bar(x, y_pos, align='center')
ax.set_yscale('log')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_xlabel('Shortest Path')
ax.set_ylabel('Number of Edges')
ax.set_title('Shortest Paths Between False Positive ' +etype+ ' Edges')
f.show()
shortest_paths_misclassified("train_four_pin_features.npy", "train_four_pin_examples.npy", "test_four_pin_features.npy", "test_four_pin_examples.npy", "Pinned")
shortest_paths_misclassified("train_four_fol_features.npy", "train_four_fol_examples.npy", "test_four_fol_features.npy", "test_four_fol_examples.npy", "Follows")
def false_negative_timestamps(train_features_file, train_example_file, test_features_file, test_example_file, attribute_file, etype):
rf = RandomForestClassifier(n_estimators=100)
#rf = MLPClassifier(hidden_layer_sizes = (100, 50, 50))
attr = numpy.load(attribute_file).item()
train_examples = numpy.load(train_features_file)
train_zip = numpy.load(train_example_file)
train_labels = [tup[1] for tup in train_zip]
test_examples = numpy.load(test_features_file)
test_zip = numpy.load(test_example_file)
test_labels = [tup[1] for tup in test_zip]
test_pairs = [tup[0] for tup in test_zip]
print 'Training model', rf
rf.fit(train_examples, train_labels)
preds = rf.predict(test_examples)
print "Getting Timestamps"
timestamps = []
for i in range(preds.size):
if preds[i] < 0 and test_labels[i] > 0:
time = attr[test_pairs[i]]['pin_time']
timestamps.append(datetime.datetime.fromtimestamp(time).isocalendar()[1])
counts = collections.Counter(timestamps)
counts = sorted(counts.items())
x = []
y = []
for c in counts:
x.append(c[0])
y.append(c[1])
min_week = min(x)
x = [w-min_week for w in x]
pylab.figure()
pylab.plot(x, y)
pylab.xlabel('Weeks In the Future')
pylab.ylabel('Number of Edges')
pylab.title('Failure to Predict ' +etype+ ' Edges Over Time')
pylab.show()
false_negative_timestamps("train_temp_pin_features.npy", "train_temp_pin_examples.npy", "test_temp_pin_features.npy", "test_temp_pin_examples.npy", "smallest_test_attr.npy", "Pinned")
false_negative_timestamps("train_temp_fol_features.npy", "train_temp_fol_examples.npy", "test_temp_fol_features.npy", "test_temp_fol_examples.npy", "smallest_test_attr.npy", "Follows")
def evolution_heat_map(train_features_file, train_example_file, test_features_file, test_example_file, etype):
bliss = EvolModel()
train_examples = numpy.load(train_features_file)
train_zip = numpy.load(train_example_file)
train_labels = [tup[1] for tup in train_zip]
test_examples = numpy.load(test_features_file)
test_zip = numpy.load(test_example_file)
test_labels = [tup[1] for tup in test_zip]
print 'Training model', bliss
bliss.fit(train_examples, train_labels)
w = bliss.w
w = numpy.array([abs(i) for i in w])
mat_len = w.size
w = numpy.append(w, w).reshape(2, mat_len)
print w
pylab.imshow(w, cmap=pylab.get_cmap("Blues"))
pylab.title('Bliss\'s Evolutionary Algorithm' +etype+ ' Feature Weights')
pylab.xlabel('Features (increasing with time)')
pylab.show()
evolution_heat_map("train_temp_pin_features.npy", "train_temp_pin_examples.npy", "test_temp_pin_features.npy", "test_temp_pin_examples.npy", "Pinned")
evolution_heat_map("train_temp_fol_features.npy", "train_temp_fol_examples.npy", "test_temp_fol_features.npy", "test_temp_fol_examples.npy", "Follows")