因为这个问题我研究了超过了三个小时,特意写下来,作为一个笔记。
如果对什么是sparse 矩阵还心存迷惑 可以参考 :
https://blog.csdn.net/pipisorry/article/details/41762945
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 |
这是四个常用的sparse矩阵储存类型。这边最常用得是 csc_matrix 和 csr_matrix (从列开始数,从行开始数)。
下面说一下用法:
# dense to sparse
from numpy import array
from scipy.sparse import csr_matrix
# create dense matrix
A = array([[1, 0, 0, 1, 0, 0], [0, 0, 2, 0, 0, 1], [0, 0, 0, 2, 0, 0]])
print(A)
# convert to sparse matrix (CSR method)
S = csr_matrix(A)
print(S)
# reconstruct dense matrix
B = S.todense()
print(B)
构造一个array矩阵,然后转成稀疏矩阵(sparse matrix)然后再转成普通矩阵(dense matrix),上面程序的运行结果是
[[1 0 0 1 0 0]
[0 0 2 0 0 1]
[0 0 0 2 0 0]]
(0, 0) 1
(0, 3) 1
(1, 2) 2
(1, 5) 1
(2, 3) 2
[[1 0 0 1 0 0]
[0 0 2 0 0 1]
[0 0 0 2 0 0]]
下面给出 另外一种构造方式,然后 在改变他的非零的值之后 再更改到稀疏矩阵
from scipy import sparse
import numpy as np
row = [1,2, 3, 3, 2]
col = [1,3, 4, 2, 3]
data = [3,8, 2, 3, 10]
c = sparse.csr_matrix((data, (row, col)), shape=(5, 5))
print (c.todense())
b= sparse.csc_matrix.todense(c)
b [2,2] =10
C1 = np.where(b>0)
b [C1] = b [C1] + 4
a = sparse.csc_matrix(b)
print(a.toarray())
输入结果
[[ 0 0 0 0 0]
[ 0 3 0 0 0]
[ 0 0 0 18 0]
[ 0 0 3 0 2]
[ 0 0 0 0 0]]
[[ 0 0 0 0 0]
[ 0 7 0 0 0]
[ 0 0 14 22 0]
[ 0 0 7 0 6]
[ 0 0 0 0 0]]
好吧 上面的那个方法比较笨,我来总结一下,这个是sparse 矩阵!!只需要更改他的data部分就好了
from scipy import sparse
import numpy as np
row = [1,2, 3, 3, 2]
col = [1,3, 4, 2, 3]
data = [3,5, 2, 3, 10]
c = sparse.csr_matrix((data, (row, col)), shape=(5, 5))
print (c.todense())
c.data = np.exp(-c.data/10)
print(c.toarray())
结果如下
[[ 0 0 0 0 0]
[ 0 3 0 0 0]
[ 0 0 0 15 0]
[ 0 0 3 0 2]
[ 0 0 0 0 0]]
[[0. 0. 0. 0. 0. ]
[0. 0.74081822 0. 0. 0. ]
[0. 0. 0. 0.22313016 0. ]
[0. 0. 0.74081822 0. 0.81873075]
[0. 0. 0. 0. 0. ]]
假设sp_a 是一个稀疏矩阵,那它的常用的计算方式是:
print sp_a += sp_a
print sp_a = sp_a*5
print sp_a.dot([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])