-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
141 lines (103 loc) · 3.81 KB
/
data_utils.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
import numpy as np
import matplotlib.pyplot as plt
def get_synthetic_dataset(**kwargs):
N = kwargs["N"]
M = kwargs["M"]
# rank of R
rank = kwargs["rank"]
# generate low-rank R
U = np.random.randn(N, rank - 1)
U = (U.T / np.linalg.norm(U, axis=1)).T
V = np.random.randn(M, rank - 1)
V = (V.T / np.linalg.norm(V, axis=1)).T
R_true = (U @ V.T + np.ones((N, M)))/2
u, s, vh = np.linalg.svd(R_true)
V_true = vh[:rank, :].T @ np.diag(np.sqrt(s[:rank]))
if kwargs.get("plot_data", False):
plt.hist(R_true.flatten(), bins=np.int32(np.sqrt(N * M)))
_, s, _ = np.linalg.svd(R_true)
plt.yscale("log")
plt.plot(s)
plt.show()
return R_true, rank, V_true
def get_restaurant_dataset(**kwargs):
R_true = np.load("data/rating_restaurant_completed.npy")
# normalize maximum value of R
R_true = R_true / 6
R_true = R_true + 0.03 * np.random.normal(size=R_true.shape)
# rank of R
rank = 10
N, M = R_true.shape
u, s, vh = np.linalg.svd(R_true)
V_true = vh[:rank, :].T @ np.diag(np.sqrt(s[:rank]))
if kwargs.get("plot_data", False):
plt.hist(R_true.flatten(), bins=np.int32(np.sqrt(N * M)))
_, s, _ = np.linalg.svd(R_true)
plt.yscale("log")
plt.plot(s)
plt.show()
return R_true, rank, V_true
def get_movie_dataset(**kwargs):
R_true = np.load("data/rating_ml_100k_completed.npy")
R_true = R_true[:300, :200]
# normalize maximum value of R
R_true = R_true / np.max(R_true)
R_true = R_true + 0.03 * np.random.normal(size=R_true.shape)
# rank of R
rank = 20
N, M = R_true.shape
u, s, vh = np.linalg.svd(R_true)
V_true = vh[:rank, :].T @ np.diag(np.sqrt(s[:rank]))
if kwargs.get("plot_data", False):
plt.hist(R_true.flatten(), bins=np.int32(np.sqrt(N * M)))
_, s, _ = np.linalg.svd(R_true)
plt.yscale("log")
plt.plot(s)
plt.show()
return R_true, rank, V_true
def get_yelp_dataset(**kwargs):
R_true = np.load("data/rating_yelp_completed.npy")
# normalize maximum value of R
R_true = R_true / np.max(R_true)
R_true = R_true[:500, :100]
# rank of R
rank = 20
N, M = R_true.shape
u, s, vh = np.linalg.svd(R_true)
V_true = vh[:rank, :].T @ np.diag(np.sqrt(s[:rank]))
if kwargs.get("plot_data", False):
plt.hist(R_true.flatten(), bins=np.int32(np.sqrt(N * M)))
_, s, _ = np.linalg.svd(R_true)
plt.yscale("log")
plt.plot(s)
plt.show()
return R_true, rank, V_true
def get_dataset(dataset=None, **kwargs):
if dataset == "synthetic":
return get_synthetic_dataset(**kwargs)
if dataset == "restaurant":
return get_restaurant_dataset(**kwargs)
if dataset == "yelp":
return get_yelp_dataset(**kwargs)
if dataset == "movie":
return get_movie_dataset(**kwargs)
def get_capacity(N, M, T, is_dynamic, cap_to_dem_ratio=1, p_activity=0.2):
if is_dynamic:
D = np.random.choice(2, size=(T, N), p=[1 - p_activity, p_activity])
# set capacities randomly (changing with time)
C_max = int(np.ceil(cap_to_dem_ratio * np.sum(D[0]) / M))
C = np.zeros((T, M), dtype=np.int64)
C[0] = np.random.choice(C_max, size=(M)) + 1
for t in range(1, T):
C[t] = np.clip(C[t - 1] + np.random.choice(3, size=(M), p=[0.05, 0.9, 0.05]) - 1, 0, 2 * C_max)
print(np.sum(C, axis=1))
else:
# set demands to 1 for all users
D = np.ones((T, N), dtype=np.int64)
# set capacities randomly (fixed in time)
C_max = int(np.ceil(cap_to_dem_ratio * np.sum(D[0]) / M))
C = np.zeros((T, M), dtype=np.int64)
C[0] = np.random.choice(C_max, size=(M)) + 1
for t in range(1, T):
C[t] = C[t - 1]
return C, D