-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo.py
53 lines (34 loc) · 844 Bytes
/
demo.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
import numpy as np
from snn import SyntheticNearestNeighbors
def normalize_rows(X):
return np.array([row/np.linalg.norm(row, 2) for row in X])
def signal_matrix(m, n, r):
# user latent features
U = np.random.randn(m, r)
U = normalize_rows(U)
# item latent features
V = np.random.randn(n, r)
V = normalize_rows(V)
# ratings
A = U @ V.T
return (A, U, V)
# model parameters
m = 100
n = 100
r = 10
# generate model
(A, U, V) = signal_matrix(m, n, r)
# observation (obfuscate last entry)
X = A.copy()
X[-1, -1] = np.nan
# estimate via SNN
params = {
'n_neighbors': 1,
'weights': 'distance',
'verbose': False
}
snn = SyntheticNearestNeighbors(**params)
X_snn = snn.fit_transform(X)
# report error
delta = np.abs(X_snn[-1,-1] - A[-1,-1])
print("SNN error = {:.2f}".format(delta))