def normalize(mx): """Row-normalize sparse matrix""" rowsum = np.array(mx.sum(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
def row_normalize(features): """Row-normalize feature matrix.""" rowsum = np.array(features.sum(1)) r_inv = np.power(rowsum, -1).flatten() r_inv[np.isinf(r_inv)] = 0. r_mat_inv = scipy.diags(r_inv) features = r_mat_inv.dot(features) return features
def normalized(adj_mat): rowsum = np.array(adj_mat.sum(1)) d_inv = np.power(rowsum, -1).flatten() d_inv[np.isinf(d_inv)] = 0. d_mat_inv = sp.diags(d_inv) norm_adj = d_mat_inv.dot(adj_mat) return norm_adj.tocsr()
def sparse_pagerank( A, beta = 0.85, one = None, niter = 1000, rel_eps = 1e-6 ) : ## Initialize the iterations one = one if one is not None else np.ones( ( 1, A.shape[ 0 ] ), dtype = np.float ) one = sp.csr_matrix( one / one.sum( axis = 1 ) ) ## Get the out-degree out = np.asarray( A.sum( axis = 1 ).getA1( ), dtype = np.float ) ## Obtain the mask of dangling vertices dangling = np.where( out == 0.0 )[ 0 ] ## Correct the out-degree for sink nodes out[ dangling ] = 1.0 ## Just one iteration: all dangling nodes add to the importance of all vertices. pi = np.full( ( one.shape[0], A.shape[0] ), 1.0 / A.shape[ 0 ], dtype = np.float ) ## If there are no dangling vertices then use simple iterations kiter, status = 0, -1 ## Make a stochastic matrix P = sp.diags( 1.0 / out, 0, dtype = np.float ).dot( A ).tocsc( ) while kiter < niter : ## make a copy of hte current ranking estimates pi_last = pi.copy( ) ## Use sparse inplace operations for speed. Firstt the random walk part pi *= beta ; pi *= P ## Now the teleportaiton ... pi += ( 1 - beta ) * one ## ... and dangling vertices part if len( dangling ) > 0 : pi += beta * one.multiply( np.sum( pi_last[ :, dangling ], axis = 1 ).reshape( ( -1, 1 ) ) ) ## Normalize pi /= np.sum( pi, axis = 1 ) if np.sum( np.abs( pi - pi_last ) ) <= one.shape[0] * rel_eps * np.sum( np.abs( pi_last ) ) : status = 0 break ## Next iteration kiter += 1 if kiter % 10 == 0 : print kiter return pi, status, kiter