bhavul
2/3/2019 - 8:08 PM

Scipy

row = [0,0,0,0,1,1,1,2,2]
col = [0,3,4,5,1,2,3,4,5]
data = [0.5,1,2,0.5,0.5,0.25,1,1,2.5]
mat = sparse.csr_matrix((data, (row,col)), shape=(3,6))
# probably it needs to be a csr matrix (not sure)
non_zero_rows_indices_list, non_zero_cols_indices_list = sparse_mat.nonzero()
# Ref : https://stackoverflow.com/questions/49254111/row-division-in-scipy-sparse-matrix

# get indices that have non-zero values
non_zero_rows_indices_list, non_zero_cols_indices_list = sparse_mat.nonzero()
# form the denominator as sparse matrix having shape same as actual matrix but values only at the respective nonzero positions
temp_deno_sparse = csr_matrix(((1.0/temp_deno)[non_zero_cols_indices], (non_zero_rows_indices,non_zero_cols_indices)), shape=user_probs.shape)
# multiply them both
user_probs_norm = user_probs.multiply(temp_deno_sparse)
sp_mat_data = sp_mat.data

# You can directly do operations on just non-zero values as well
user_level_cumulated_probs.data = (1 - user_level_cumulated_probs.data)
# You need to have a csr matrix. If you don't have that convert to csr
[user_row.data for user_row in user_level_cumulated_probs]