def normalize_sparse(M, norm="<STR_LIT>", order=<NUM_LIT:1>, iterations=<NUM_LIT:3>):
try:<EOL><INDENT>from scipy.sparse import csr_matrix<EOL><DEDENT>except ImportError as e:<EOL><INDENT>print(str(e))<EOL>print("<STR_LIT>")<EOL>return normalize_dense(M.todense())<EOL><DEDENT>r = csr_matrix(M)<EOL>if norm == "<STR_LIT>":<EOL><INDENT>for _ in range(<NUM_LIT:1>, iterations):<EOL><INDENT>row_sums = np.array(r.sum(axis=<NUM_LIT:1>)).flatten()<EOL>col_sums = np.array(r.sum(axis=<NUM_LIT:0>)).flatten()<EOL>row_indices, col_indices = r.nonzero()<EOL>r.data /= row_sums[row_indices] * col_sums[col_indices]<EOL><DEDENT><DEDENT>elif norm == "<STR_LIT>":<EOL><INDENT>try:<EOL><INDENT>from scipy.sparse import linalg<EOL>r = linalg.norm(M, ord=order)<EOL><DEDENT>except (ImportError, AttributeError) as e:<EOL><INDENT>print(str(e))<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL><DEDENT><DEDENT>elif callable(norm):<EOL><INDENT>r = norm(M)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>return r<EOL>
Applies a normalization type to a sparse matrix.