-
Notifications
You must be signed in to change notification settings - Fork 0
/
cur.py
84 lines (67 loc) · 2 KB
/
cur.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
import numpy as np
import pandas as pd
from scipy import sparse as sp
MAX_MID = 27277 + 1
def select_cols(mat, k, dup=False):
# prob 1d array of probabilities of all columns
prob = mat.T.dot(mat)
prob = np.array(np.diagonal(prob))
denom = np.abs(prob).sum(axis = 0)
prob = prob/denom
C = np.zeros((mat.shape[0], k))
ind_cols = np.arange(0, prob.size)
c_ind = []
for i in range(k):
rand_sel = np.random.choice(ind_cols, 1, p=prob)
c_ind.append(rand_sel[0])
C[:, i] = mat[:, rand_sel[0]]
C[:, i] = C[:, i]/np.sqrt(k*prob[rand_sel[0]])
return C, c_ind
def select_rows(mat, k, dup=False):
prob = mat.dot(mat.T)
prob = np.array(np.diagonal(prob))
denom = np.abs(prob).sum(axis=0)
prob = prob/denom
print(prob)
R = np.zeros((k, mat.shape[1]))
ind_rows = np.arange(0, prob.size)
r_ind = []
for i in range(k):
rand_sel = np.random.choice(ind_rows, 1, p=prob)
r_ind.append(rand_sel[0])
R[i, :] = mat[rand_sel[0], :]
R[i, :] = R[i, :]/np.sqrt(k*prob[rand_sel[0]])
r_ind = np.array(r_ind)
return R, r_ind
def matIntersection(mat, c_ind, r_ind):
W = np.zeros((len(r_ind), len(c_ind)))
for i in range(len(r_ind)):
W[i] = mat[r_ind[i], c_ind]
return W
def pseudoInverse(W):
# U = WP (W+)
# W = X.Z.YT
X, Z, YT = np.linalg.svd(W)
# W+ = Y.Z+.XT
XT = X.T
Y = YT.T
# Z+ = reciprocal(Z)
ZP = np.reciprocal(Z)
ZP = sp.spdiags(ZP, 0, ZP.size, ZP.size)
ZP = ZP@ZP
# W+ = Y.Z+.XT
WP = Y@ZP
WP = WP@XT
return WP
def main():
mat = np.array([[0, 1, 2], [2, 0, 4], [1, 3, 2]])
print(mat)
C, c_ind = select_cols(mat, 2)
R, r_ind= select_rows(mat, 2)
W = matIntersection(mat, c_ind, r_ind)
U = pseudoInverse(W)
Pred = C@U@R
print(Pred)
print("Finished Execution")
if __name__ == "__main__":
main()