numpy如何变换。稀疏矩阵或数组到scipy矩阵

时间:2022-01-10 21:21:04

For SciPy sparse matrix, one can use todense() or toarray() to transform to NumPy matrix or array. What are the functions to do the inverse?

对于SciPy稀疏矩阵,可以使用todensity()或toarray()将其转换为NumPy矩阵或数组。求逆的函数是什么?

I searched, but got no idea what keywords should be the right hit.

我搜索了一下,但是不知道什么关键字应该是正确的。

3 个解决方案

#1


71  

You can pass a numpy array or matrix as an argument when initializing a sparse matrix. For a CSR matrix, for example, you can do the following.

在初始化稀疏矩阵时,可以将numpy数组或矩阵作为参数传递。例如,对于CSR矩阵,您可以执行以下操作。

>>> import numpy as np
>>> from scipy import sparse
>>> A = np.array([[1,2,0],[0,0,3],[1,0,4]])
>>> B = np.matrix([[1,2,0],[0,0,3],[1,0,4]])

>>> A
array([[1, 2, 0],
       [0, 0, 3],
       [1, 0, 4]])

>>> sA = sparse.csr_matrix(A)   # Here's the initialization of the sparse matrix.
>>> sB = sparse.csr_matrix(B)

>>> sA
<3x3 sparse matrix of type '<type 'numpy.int32'>'
        with 5 stored elements in Compressed Sparse Row format>

>>> print sA
  (0, 0)        1
  (0, 1)        2
  (1, 2)        3
  (2, 0)        1
  (2, 2)        4

#2


17  

There are several sparse matrix classes in scipy.

scipy中有几个稀疏矩阵类。

bsr_matrix(arg1[, shape, dtype, copy, blocksize]) Block Sparse Row matrix
coo_matrix(arg1[, shape, dtype, copy]) A sparse matrix in COOrdinate format.
csc_matrix(arg1[, shape, dtype, copy]) Compressed Sparse Column matrix
csr_matrix(arg1[, shape, dtype, copy]) Compressed Sparse Row matrix
dia_matrix(arg1[, shape, dtype, copy]) Sparse matrix with DIAgonal storage
dok_matrix(arg1[, shape, dtype, copy]) Dictionary Of Keys based sparse matrix.
lil_matrix(arg1[, shape, dtype, copy]) Row-based linked list sparse matrix

bsr_matrix(arg1[, shape, dtype, copy, blocksize])块稀疏行矩阵coo_matrix(arg1[, shape, dtype, copy])稀疏列矩阵(arg1[, shape, dtype, copy])压缩稀疏列矩阵csr_matrix(arg1[, shape, dtype, copy])压缩行矩阵dia_matrix(arg1[, shape, dtype, copy])lil_matrix(arg1[, shape, dtype, copy])基于行的链表稀疏矩阵

Any of them can do the conversion.

它们中的任何一个都可以进行转换。

import numpy as np
from scipy import sparse
a=np.array([[1,0,1],[0,0,1]])
b=sparse.csr_matrix(a)
print(b)

(0, 0)  1
(0, 2)  1
(1, 2)  1

See http://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information .

看到http://docs.scipy.org/doc/scipy/reference/sparse.html使用信息。

#3


1  

As for the inverse, the function is inv(A), but I won't recommend using it, since for huge matrices it is very computationally costly and unstable. Instead, you should use an approximation to the inverse, or if you want to solve Ax = b you don't really need A-1.

至于逆函数,它是inv(A),但我不建议使用它,因为对于大型矩阵,它的计算开销非常大,而且不稳定。相反,你应该使用逆的近似,或者如果你想解出Ax = b你不需要A-1。

#1


71  

You can pass a numpy array or matrix as an argument when initializing a sparse matrix. For a CSR matrix, for example, you can do the following.

在初始化稀疏矩阵时,可以将numpy数组或矩阵作为参数传递。例如,对于CSR矩阵,您可以执行以下操作。

>>> import numpy as np
>>> from scipy import sparse
>>> A = np.array([[1,2,0],[0,0,3],[1,0,4]])
>>> B = np.matrix([[1,2,0],[0,0,3],[1,0,4]])

>>> A
array([[1, 2, 0],
       [0, 0, 3],
       [1, 0, 4]])

>>> sA = sparse.csr_matrix(A)   # Here's the initialization of the sparse matrix.
>>> sB = sparse.csr_matrix(B)

>>> sA
<3x3 sparse matrix of type '<type 'numpy.int32'>'
        with 5 stored elements in Compressed Sparse Row format>

>>> print sA
  (0, 0)        1
  (0, 1)        2
  (1, 2)        3
  (2, 0)        1
  (2, 2)        4

#2


17  

There are several sparse matrix classes in scipy.

scipy中有几个稀疏矩阵类。

bsr_matrix(arg1[, shape, dtype, copy, blocksize]) Block Sparse Row matrix
coo_matrix(arg1[, shape, dtype, copy]) A sparse matrix in COOrdinate format.
csc_matrix(arg1[, shape, dtype, copy]) Compressed Sparse Column matrix
csr_matrix(arg1[, shape, dtype, copy]) Compressed Sparse Row matrix
dia_matrix(arg1[, shape, dtype, copy]) Sparse matrix with DIAgonal storage
dok_matrix(arg1[, shape, dtype, copy]) Dictionary Of Keys based sparse matrix.
lil_matrix(arg1[, shape, dtype, copy]) Row-based linked list sparse matrix

bsr_matrix(arg1[, shape, dtype, copy, blocksize])块稀疏行矩阵coo_matrix(arg1[, shape, dtype, copy])稀疏列矩阵(arg1[, shape, dtype, copy])压缩稀疏列矩阵csr_matrix(arg1[, shape, dtype, copy])压缩行矩阵dia_matrix(arg1[, shape, dtype, copy])lil_matrix(arg1[, shape, dtype, copy])基于行的链表稀疏矩阵

Any of them can do the conversion.

它们中的任何一个都可以进行转换。

import numpy as np
from scipy import sparse
a=np.array([[1,0,1],[0,0,1]])
b=sparse.csr_matrix(a)
print(b)

(0, 0)  1
(0, 2)  1
(1, 2)  1

See http://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information .

看到http://docs.scipy.org/doc/scipy/reference/sparse.html使用信息。

#3


1  

As for the inverse, the function is inv(A), but I won't recommend using it, since for huge matrices it is very computationally costly and unstable. Instead, you should use an approximation to the inverse, or if you want to solve Ax = b you don't really need A-1.

至于逆函数,它是inv(A),但我不建议使用它,因为对于大型矩阵,它的计算开销非常大,而且不稳定。相反,你应该使用逆的近似,或者如果你想解出Ax = b你不需要A-1。