-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithm.py
75 lines (58 loc) · 1.83 KB
/
Algorithm.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
import random
import numpy as np
def MinMaxScaler(data):
"""Min Max normalizer.
Args:
- data: original data
Returns:
- norm_data: normalized data
"""
numerator = data - np.min(data, 0)
denominator = np.max(data, 0) - np.min(data, 0)
norm_data = numerator / (denominator + 1e-7)
return norm_data
def order(bins, bin_size, n):
"""Algorithm to order ids of businesses.
Args:
- bins: list of numpy arrays of ids
- bin_size: number of bins
- n: number of businesses
Returns:
- ordered: ordered list of ids
"""
choices = range(bin_size)
dist = [(bin_size - choice) ** 2 for choice in choices]
count = 0
ordered = list()
while count < n:
random_bin = random.choices(population=choices, weights=dist)[0]
bn = bins[random_bin]
if bn is not None and len(bn) != 0:
elt = bn.item(0)
ordered.append(elt)
bins[random_bin] = np.delete(bn, 0)
count += 1
return ordered
def reorder(num_list, id_list, n):
"""Reorderer of list of businesses to promote lower values.
Args:
- num_list: list of business' number of reviews
- id_list: list of business' ids
- size: number of businesses
Returns:
- result: reordered list of ids
"""
nums = np.array(num_list)
ids = np.array(id_list, dtype=object)
bin_size = max(n // 15, 1)
scaled = MinMaxScaler(nums)
bins = list()
bn = ids[scaled <= (1 / bin_size)]
np.random.shuffle(bn)
bins.append(bn)
for i in range(1, bin_size):
bn = ids[((i / bin_size) < scaled) & (scaled <= ((i + 1) / bin_size))]
np.random.shuffle(bn)
bins.append(bn)
result = order(bins, bin_size, n)
return result