Skip to content

Commit 6e7734f

Browse files
committed
Added RMR
1 parent 32e5626 commit 6e7734f

3 files changed

Lines changed: 932 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
import math
3+
import scipy
4+
5+
6+
def get_sparse_only(matrix, threshold_perc, compression='naive'):
7+
threshold = np.max(matrix) * threshold_perc*0.01
8+
if compression == 'rmr':
9+
copy_matrix = matrix.copy()
10+
print('Generating sparse matrix using RMR...')
11+
np.apply_along_axis(row_operation, 1, copy_matrix, threshold)
12+
S = scipy.sparse.csr_matrix(copy_matrix)
13+
else:
14+
S = np.where(matrix >= threshold, matrix, 0)
15+
S = scipy.sparse.csr_matrix(S)
16+
return S
17+
18+
19+
def row_operation(copy_row, threshold):
20+
argzero = np.argwhere((np.abs(copy_row) <= threshold) * (copy_row != 0))
21+
argzero = argzero.reshape(len(argzero), )
22+
argzero_copy = copy_row[argzero]
23+
copy_row[argzero] = 0
24+
sum = np.sum(argzero_copy)
25+
if sum != 0:
26+
k = math.ceil(sum / threshold)
27+
28+
indices = np.random.choice(argzero, k, p=argzero_copy / sum, replace=True)
29+
np.add.at(copy_row, indices, sum / k)
30+

0 commit comments

Comments
 (0)