-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_regression_data.py
170 lines (129 loc) · 4.09 KB
/
extract_regression_data.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
from inverse_problem import (
aggregate_recommendations,
count_occurrences,
get_popularity_bias_denominator,
get_popularity_bias_range,
get_release_recency_bias_range,
summarize_occurrences,
)
def identify_common_bias(bias_val, bias_occurrences):
n = max(bias_occurrences)
indices = [
ind for ind in range(len(bias_occurrences)) if bias_occurrences[ind] == n
]
bias_argmax_list = [bias_val[ind] for ind in indices]
return bias_argmax_list, n
def extract_data_with_equal_release_recency_bias(
app_id,
aggregated_recommendations,
rb_occurrences_dict,
verbose=False,
):
rb_argmax_list, n = identify_common_bias(
get_release_recency_bias_range(),
rb_occurrences_dict[str(app_id)],
)
data = {}
for rb_argmax in rb_argmax_list:
if verbose:
print(f'\nAppID = {app_id}')
print(
'Extracting data with release recency bias equal to {} months.'.format(
rb_argmax,
),
)
X = []
y = []
for elem in aggregated_recommendations[str(app_id)]:
if elem['release_bias'] == rb_argmax:
x_i = elem['popularity_bias'] / get_popularity_bias_denominator()
y_i = elem['tweaked_score']
X.append(x_i)
y.append(y_i)
if verbose:
print(f'X = {X}')
print(f'y = {y}')
if len(y) != n:
raise AssertionError()
data[str(rb_argmax)] = {"X": X, "y": y}
return data
def extract_data_with_equal_popularity_bias(
app_id,
aggregated_recommendations,
pb_occurrences_dict,
verbose=False,
):
pb_argmax_list, n = identify_common_bias(
get_popularity_bias_range(),
pb_occurrences_dict[str(app_id)],
)
data = {}
for pb_argmax in pb_argmax_list:
if verbose:
print(f'\nAppID = {app_id}')
print(
'Extracting data with popularity bias equal to {}/{}.'.format(
pb_argmax,
get_popularity_bias_denominator(),
),
)
X = []
y = []
for elem in aggregated_recommendations[str(app_id)]:
if elem['popularity_bias'] == pb_argmax:
x_i = elem['release_bias']
y_i = elem['tweaked_score']
X.append(x_i)
y.append(y_i)
if verbose:
print(f'X = {X}')
print(f'y = {y}')
if len(y) != n:
raise AssertionError()
data[str(pb_argmax)] = {"X": X, "y": y}
return data
def extract_data(app_id, aggregated_recommendations, verbose=False):
if verbose:
print(f'\nAppID = {app_id}')
print('Extracting all available data.')
X = []
y = []
for elem in aggregated_recommendations[str(app_id)]:
x_i = [
elem['popularity_bias'] / get_popularity_bias_denominator(),
elem['release_bias'],
]
y_i = elem['tweaked_score']
X.append(x_i)
y.append(y_i)
if verbose:
print(f'X = {X}')
print(f'y = {y}')
data = {"X": X, "y": y}
return data
def main():
aggregated_recommendations = aggregate_recommendations(verbose=False)
stats = count_occurrences(aggregated_recommendations, verbose=False)
app_ids, pb_occurrences_dict, rb_occurrences_dict = summarize_occurrences(
aggregated_recommendations,
stats,
chosen_num_occurrences=max(stats.keys()),
verbose=True,
)
for app_id in app_ids:
data = extract_data_with_equal_release_recency_bias(
app_id,
aggregated_recommendations,
rb_occurrences_dict,
verbose=True,
)
data = extract_data_with_equal_popularity_bias(
app_id,
aggregated_recommendations,
pb_occurrences_dict,
verbose=True,
)
data = extract_data(app_id, aggregated_recommendations, verbose=True)
return
if __name__ == '__main__':
main()