scipy读取稀疏数据

转自:https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.coo_matrix.tocsr.html

https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.mmread.html

1.基本用法

>>> from numpy import array
>>> from scipy.sparse import coo_matrix
>>> row  = array([0,0,1,3,1,0,0])
>>> col  = array([0,2,1,3,1,0,0])
>>> data = array([1,1,1,1,1,1,1])
>>> A = coo_matrix( (data,(row,col)), shape=(4,4)).tocsr()
>>> A.todense()
matrix([[3, 0, 1, 0],
        [0, 2, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 1]])

直接根据行index,列index,以及data建立索引,A就是稀疏数据的类型:

<4x4 sparse matrix of type '<class 'numpy.float32'>'
    with 3 stored elements in Compressed Sparse Row format>

压缩稀疏行格式。

2.从文件中读取mtx格式数据并存储为稀疏格式

count内容:

%%MatrixMarket matrix coordinate integer general
4 4 4
1 1 1
1 2 1
1 1 1
2 3 1

这里针对第一行,应该是mtx文件的必要格式,需要5列内容。第二行表示的意思分别是:cols,rows,total_nums。

注意行号和列号都不是从0开始,而是从1,这和计算机中的习惯并不一样,大概是为了更多领域来用这个包吧。

from numpy import array
from scipy.io import mmread
count = mmread('a.mtx').T.tocsr().astype('float32')

#输出:
>>> count
<4x4 sparse matrix of type '<class 'numpy.float32'>'
    with 3 stored elements in Compressed Sparse Row format>
>>> count.todense()
matrix([[2., 0., 0., 0.],
        [1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 0., 0.]], dtype=float32)

3.对稀疏矩阵取log 

from numpy import array
import numpy as np
from scipy.sparse import coo_matrix,csr_matrix
row  = array([0,0,1,3,1,0,0])
col  = array([0,2,1,3,1,0,0])
data = array([1,1,1,1,1,1,1])
A = coo_matrix( (data,(row,col)), shape=(4,4)).tocsr()
print(A.todense())
#A=np.log(1+A)#这样会报错
#NotImplementedError: adding a nonzero scalar to a sparse matrix is not supported
A=csr_matrix(np.log(A.toarray() + 1))
print(A.todense())

不能直接取log,会报错。

需要转换为array后操作,再转换为array。

4.对稀疏矩阵的转置

接上面的代码,可以直接使用

A=np.transpose(A)

#输出:
[[3 0 1 0]
 [0 2 0 0]
 [0 0 0 0]
 [0 0 0 1]]

[[3 0 0 0]
 [0 2 0 0]
 [1 0 0 0]
 [0 0 0 1]]

不需要转换为array就能用np的转置函数。

5.array转换为稀疏格式 

https://blog.csdn.net/weixin_32597695/article/details/113489657

6.稀疏矩阵的合并 

https://blog.csdn.net/qq_16912257/article/details/70888740

  • 横向合并: scipy.sparse.hstack
  • 纵向合并: scipy.sparse.vstack
>>> from scipy.sparse import hstack,vstack,csc_matrix
>>> import numpy as np
>>> X = csc_matrix(np.arange(4).reshape(2, 2))
>>> X.toarray()
array([[0, 1],
       [2, 3]])
>>> Y = csc_matrix(np.arange(4, 8).reshape(2, 2))
>>> Y.toarray()
array([[4, 5],
       [6, 7]])
>>> hstack((X, Y)).toarray()
array([[0, 1, 4, 5],
       [2, 3, 6, 7]])
>>> vstack((X, Y)).toarray()
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])

7.访问稀疏矩阵的元素 

http://cn.voidcc.com/question/p-fzkdhesx-boc.html

from scipy.sparse import coo_matrix
m = coo_matrix([[1, 2, 3], [4, 5, 6]]) 
m1 = m.tocsr()
print(m.toarray())
print(m1.toarray())

 

coo_matrix稀疏类型不能按照下标访问,需要转换为crs系数格式存储:

[[1 2 3]
 [4 5 6]]
[[1 2 3]
 [4 5 6]]
>>> m[1,2]
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    m[1,2]
TypeError: 'coo_matrix' object is not subscriptable
>>> m1[1,2]
6

 

posted @ 2020-03-05 16:03  lypbendlf  阅读(1698)  评论(0编辑  收藏  举报