-
Notifications
You must be signed in to change notification settings - Fork 0
/
L_S_decomp.py
64 lines (53 loc) · 1.67 KB
/
L_S_decomp.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
# Generating a matrix with corrupted rows/columns
import numpy as np
from numpy import linalg as LA
from random import *
from math import *
def LowRank_Sparse(m, n, sparse_depth, byRow=True):
X = np.random.normal(0, 1, (m, n))
Y = np.random.normal(0, 1, (m, n))
L_0 = X@Y.T
S_0 = np.zeros((m, m))
x = sample(range(m), k=ceil(sparse_depth*m))
if byRow==True:
S_0[x, :] = np.random.normal(20, 5, m)
else:
S_0[:, x] = np.random.normal(20, 5, m)
M = L_0+S_0
return M, L_0, S_0
def LowRank_Sparse_Block(m, n, sparse_depth):
X = np.random.normal(0, 1, (m, n))
Y = np.random.normal(0, 1, (m, n))
L_0 = X@Y.T
S_0 = np.zeros((m, m))
x = ceil(m/2)
a = ceil(x+(sparse_depth*m))
for i in range(x, a):
for j in range(x, a):
S_0[i, j] = np.random.normal(20, 5)
M = L_0+S_0
return M, L_0, S_0
def is_all_zero(C):
n = C.shape[0]
count = 0
for i in range(n):
for j in range(n):
if C[i, j] != 0:
count=count+1
else:
count=count
return count
# Sparse depth is an int for the number of nonzero entries in S
# c > 0
# Setting used http://jmlr.org/papers/volume20/18-022/18-022.pdf on page 11
def l_s(m, n, sparse_depth, c):
X = np.random.normal(0, 1, (m, n))
Y = np.random.normal(0, 1, (m, n))
L_0 = X@Y.T
S_0 = np.zeros((m, m))
rows = np.random.choice(m, sparse_depth)
cols = np.random.choice(m, sparse_depth)
for i in range(sparse_depth):
S_0[rows[i], cols[i]] = np.random.uniform(-c*np.abs(L_0[rows[i], cols[i]]), c*np.abs(L_0[rows[i], cols[i]]))
M = L_0 + S_0
return M, L_0, S_0