11/02/2020

sci sparse -> tuple list -> sci sparse

 refer to below source code


source code start


from scipy.sparse import csr_matrix
#sci sparse to tuple list
c = A2.tocoo() #A2 is scipy.sparse.csr.csr_matrix
in_edge_idx = list(zip(c.row, c.col)) #make tuple list

#tuple list to sci sparse
two_list = list(map(list, zip(*in_edge_idx))) #tuple 2 tow list of list [[1,2,3], [2,3,4]]
rows = np.array(two_list[0]) #rows
cols = np.array(two_list[1]) #cols
data_num = len(rows) #number of edge
data = np.ones( data_num ) #edge value
dim = len(x_data) #N x N adj

#sci sparse -> tuple list -> sci sparse
re_edge_idx = csr_matrix((data, (rows, cols)), shape=(dim, dim))

print('in', A2, type(A2))
print('re', re_edge_idx, type(re_edge_idx))

#origin A2 and re-generated edge index same?
print( (A2!=re_edge_idx).nnz==0 )

source code end




No comments:

Post a Comment