[英]How to transform numpy.matrix or array to scipy sparse matrix
对于 SciPy 稀疏矩阵,可以使用todense()
或toarray()
转换为 NumPy 矩阵或数组。 做逆运算的函数是什么?
我搜索过,但不知道哪些关键字应该是正确的。
初始化稀疏矩阵时,您可以将 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
scipy 中有几个稀疏矩阵类。
bsr_matrix(arg1[, shape, dtype, copy, blocksize]) 块稀疏行矩阵
coo_matrix(arg1[, shape, dtype, copy]) 坐标格式的稀疏矩阵。
csc_matrix(arg1[, shape, dtype, copy]) 压缩稀疏列矩阵
csr_matrix(arg1[, shape, dtype, copy]) 压缩稀疏行矩阵
dia_matrix(arg1[, shape, dtype, copy]) 具有对角存储的稀疏矩阵
dok_matrix(arg1[, shape, dtype, copy]) 基于密钥字典的稀疏矩阵。
lil_matrix(arg1[, shape, dtype, copy]) 基于行的链表稀疏矩阵
他们中的任何一个都可以进行转换。
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
请参阅http://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information 。
至于逆,函数是inv(A)
,但我不建议使用它,因为对于巨大的矩阵,它的计算成本非常高且不稳定。 相反,您应该使用倒数的近似值,或者如果您想求解 Ax = b ,则您实际上并不需要 A -1 。
在 Python 中,可以使用Scipy 库将二维 NumPy 矩阵转换为稀疏矩阵。 用于数值数据的 SciPy 二维稀疏矩阵包是scipy.sparse
scipy.sparse 包提供了不同的类来从二维矩阵创建以下类型的稀疏矩阵:
CSR (压缩稀疏行)或CSC (压缩稀疏列)格式支持高效访问和矩阵运算。
使用 Scipy 类将 Numpy 矩阵转换为压缩稀疏列 (CSC) 矩阵和压缩稀疏行 (CSR) 矩阵的示例代码:
import sys # Return the size of an object in bytes
import numpy as np # To create 2 dimentional matrix
from scipy.sparse import csr_matrix, csc_matrix
# csr_matrix: used to create compressed sparse row matrix from Matrix
# csc_matrix: used to create compressed sparse column matrix from Matrix
创建一个二维 Numpy 矩阵
A = np.array([[1, 0, 0, 0, 0, 0],\
[0, 0, 2, 0, 0, 1],\
[0, 0, 0, 2, 0, 0]])
print("Dense matrix representation: \n", A)
print("Memory utilised (bytes): ", sys.getsizeof(A))
print("Type of the object", type(A))
打印矩阵和其他详细信息:
Dense matrix representation:
[[1 0 0 0 0 0]
[0 0 2 0 0 1]
[0 0 0 2 0 0]]
Memory utilised (bytes): 184
Type of the object <class 'numpy.ndarray'>
使用 csr_matrix 类将矩阵 A 转换为压缩稀疏行矩阵表示:
S = csr_matrix(A)
print("Sparse 'row' matrix: \n",S)
print("Memory utilised (bytes): ", sys.getsizeof(S))
print("Type of the object", type(S))
打印语句的输出:
Sparse 'row' matrix:
(0, 0) 1
(1, 2) 2
(1, 5) 1
(2, 3) 2
Memory utilised (bytes): 56
Type of the object: <class 'scipy.sparse.csr.csc_matrix'>
使用 csc_matrix 类将矩阵 A 转换为压缩稀疏列矩阵表示:
S = csc_matrix(A)
print("Sparse 'column' matrix: \n",S)
print("Memory utilised (bytes): ", sys.getsizeof(S))
print("Type of the object", type(S))
打印语句的输出:
Sparse 'column' matrix:
(0, 0) 1
(1, 2) 2
(2, 3) 2
(1, 5) 1
Memory utilised (bytes): 56
Type of the object: <class 'scipy.sparse.csc.csc_matrix'>
可以看出,压缩矩阵的大小为 56 字节,原始矩阵大小为 184 字节。
更详细的解释和代码示例请参考这篇文章: https : //limitlessdatascience.wordpress.com/2020/11/26/sparse-matrix-in-machine-learning/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.