numpy数组到scipy.sparse矩阵

cls*_*udt 5 numpy scipy sparse-matrix python-3.x

给定一个任意的numpy数组(ndarray),是否有函数或简短的方法将其转换为scipy.sparse矩阵?

我想要一些像以下一样的东西:

A = numpy.array([0,1,0],[0,0,0],[1,0,0])
S = to_sparse(A, type="csr_matrix")
Run Code Online (Sandbox Code Playgroud)

DSM*_*DSM 6

我经常这样做

>>> import numpy, scipy.sparse
>>> A = numpy.array([[0,1,0],[0,0,0],[1,0,0]])
>>> Asp = scipy.sparse.csr_matrix(A)
>>> Asp
<3x3 sparse matrix of type '<type 'numpy.int64'>'
    with 2 stored elements in Compressed Sparse Row format>
Run Code Online (Sandbox Code Playgroud)