-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordVecHistogramsJaccard.py
74 lines (71 loc) · 1.98 KB
/
wordVecHistogramsJaccard.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
import matplotlib.pyplot as plt
from dbHelper import *
import jaccard
import collections
import numpy as np
# calculatedSet = set()
# allJacSims = []
# print "Finding jaccard simularities between friends"
# with open("graphAttributes.txt", "r") as f:
# for line in f:
# split = line.split("|")
# user_id = split[0]
# friends = split[2].split(",")
# if (len(friends[0]) == 0): continue
# for user2 in friends:
# if (user_id, user2) not in calculatedSet:
# jacSim = jaccard.getJacSim(user_id, user2)
# if jacSim is not None:
# allJacSims.append(jacSim)
# calculatedSet.add((user_id, user2))
# calculatedSet.add((user2, user_id))
# plt.figure(1, facecolor='white')
# n, bins, patches = plt.hist(allJacSims)#, 50, normed=1)
# print "n"
# print n
# print ""
# print "bins"
# print bins
# plt.xlabel('Jaccard Similarities')
# plt.ylabel('Counts')
# plt.title('Jaccard Similarities of Friends')
# # plt.axis([0.6, 1.0, 0, 7000])
# plt.grid(True)
# plt.show()
numPairs = 0
randomPairs = set()
uids = set()
allJacSims = []
print "Finding jaccard simularities between friends"
with open("graphAttributes.txt", "r") as f:
for line in f:
split = line.split("|")
user_id = split[0]
friends = len(split[2].split(","))
numPairs += friends
uids.add(user_id)
uids = list(uids)
numPairs /= 2
while len(randomPairs) < numPairs:
randPair = np.random.choice(uids, 2, replace=False)
while (randPair[0], randPair[1]) in randomPairs:
randPair = np.random.choice(uids, 2, replace=False)
jacSim = jaccard.getJacSim(randPair[0], randPair[1])
if jacSim is not None:
allJacSims.append(jacSim)
randomPairs.add((randPair[0], randPair[1]))
randomPairs.add((randPair[1], randPair[0]))
print "Done"
plt.figure(1, facecolor='white')
n, bins, patches = plt.hist(allJacSims, 30)
print "n"
print n
print ""
print "bins"
print bins
plt.xlabel('Jaccard Similarities')
plt.ylabel('Counts')
plt.title('Jaccard Similarities of Random Pairs')
# plt.axis([0.6, 1.0, 0, 7000])
plt.grid(True)
plt.show()