怎么把0很多的numpy array转成scipy里的稀疏矩阵?
1个回答
>>> import numpy as np
>>> from scipy import sparse
>>> arr = np.array([[0,2,0],[0,0,3],[1,0,0],[0,0,4]])
# numpy array转成稀疏
>>> arr_sparse = sparse.csr_matrix(arr)
# 稀疏矩阵转成numpy array
>>> arr_numpy = arr_sparse.A
SofaSofa数据科学社区DS面试题库 DS面经