#!/usr/bin/env python # -*- coding: utf-8 -*- """ This file re-uses implementation from https://github.com/yl-1993/learn-to-cluster """ import numpy as np import scipy.sparse as sp from scipy.sparse import coo_matrix def row_normalize(mx): """Row-normalize sparse matrix""" rowsum = np.array(mx.sum(1)) # if rowsum <= 0, keep its previous value rowsum[rowsum <= 0] = 1 r_inv = np.power(rowsum, -1).flatten() r_inv[np.isinf(r_inv)] = 0. r_mat_inv = sp.diags(r_inv) mx = r_mat_inv.dot(mx) return mx, r_inv def sparse_mx_to_indices_values(sparse_mx): sparse_mx = sparse_mx.tocoo().astype(np.float32) indices = np.vstack((sparse_mx.row, sparse_mx.col)).astype(np.int64) values = sparse_mx.data shape = np.array(sparse_mx.shape) return indices, values, shape