-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.py
More file actions
executable file
·66 lines (51 loc) · 1.6 KB
/
sim.py
File metadata and controls
executable file
·66 lines (51 loc) · 1.6 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
import matplotlib.pyplot as plt
import numpy as np
from numpy import linalg
import warnings
def main():
vec = {id : np.zeros(61068) for id in range(20)}
id = -1
names = []
with open("usenet.txt", "r") as f:
for line in f:
if line[-2] == ':' and line[0] != ' ':
id += 1
names.append(line[:-2])
elif ": " in line.strip():
i = line.strip().index(": ")
if i != -1:
word = int(line.strip()[0:i])
num = int(line.strip()[i + 2:])
vec[id][word] += num
data = np.ndarray((20, 20))
for i in range(20):
for j in range(20):
data[i, j] = cos(vec[i], vec[j])
makeHeatMap(data, names, "seismic")
def jac(x, y):
return np.sum(np.minimum(x, y)) / np.sum(np.maximum(x, y))
def l2(x, y):
return - np.linalg.norm(x - y, ord=2)
def cos(x, y):
return np.dot(x, y) / np.linalg.norm(x, ord=2) / np.linalg.norm(y, ord=2)
def makeHeatMap(data, names, color):
#to catch "falling back to Agg" warning
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fig, ax = plt.subplots()
#create the map w/ color bar legend
heatmap = ax.pcolor(data, cmap=color)
cbar = plt.colorbar(heatmap)
# put the major ticks at the middle of each cell
ax.set_xticks(np.arange(data.shape[0])+0.5, minor=False)
ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
# want a more natural, table-like display
ax.invert_yaxis()
ax.xaxis.tick_top()
ax.set_xticklabels(range(1, 21))
ax.set_yticklabels(names)
plt.tight_layout()
plt.show()
plt.close()
if __name__ == "__main__":
main()