-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimilarity_of_poi_flowdata.py
More file actions
35 lines (32 loc) · 1.13 KB
/
similarity_of_poi_flowdata.py
File metadata and controls
35 lines (32 loc) · 1.13 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
import numpy as np
def cossimi(a, b):
n = len(a)
dot = sum([a[i] * b[i] for i in range(n)])
a_len = np.sqrt(sum([a[i] * a[i] for i in range(n)]))
b_len = np.sqrt(sum([b[i] * b[i] for i in range(n)]))
if a_len == 0 or b_len == 0:
return 0.0
return dot / (a_len * b_len)
def POI_similarity(POI):
data = POI
type_poi, side, _ = data.shape
poi = data.reshape(type_poi, -1)
# calculate the similarity of different region
poi_similar_index = []
pearson_similar_value = []
for i in range(side * side):
max_pearson = 0.0
most_similar_region = 0
for j in range(side * side):
# avoid to calculate self pearson
if i == j:
continue
poi_a_flag = poi[:, i]
poi_b_flag = poi[:, j]
pearsons = cossimi(poi_a_flag, poi_b_flag)
if np.abs(pearsons) > max_pearson:
max_pearson = pearsons
most_similar_region = j
poi_similar_index.append(int(most_similar_region))
pearson_similar_value.append(max_pearson)
return poi_similar_index, pearson_similar_value