[英]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.