简体   繁体   English

python稀疏csr矩阵:如何序列化它

[英]python sparse csr matrix: how to serialize it

I have a csr_matrix, which is constructed as follows: 我有一个csr_matrix,其构造如下:

from scipy.sparse import csr_matrix
import numpy as np
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
a = csr_matrix((data, (row, col)), shape=(3, 3))

Now to serialize (and for some other purpose), I want to get row, col and data information from matrix "a". 现在要序列化(以及其他目的),我想从矩阵“a”获取行,列和数据信息。

Kindly tell me an easy way to achieve it. 请告诉我一个简单的方法来实现它。

Edit: a.data will give me the data, but how to get row and col informaion 编辑:a.data会给我数据,但是如何获取行和col信息

coo format has the values that you want: coo格式具有您想要的值:

In [3]: row = np.array([0, 0, 1, 2, 2, 2])   
In [4]: col = np.array([0, 2, 2, 0, 1, 2])
In [5]: data = np.array([1, 2, 3, 4, 5, 6])
In [6]: a = sparse.csr_matrix((data,(row,col)), shape=(3,3))

In [7]: a.data
Out[7]: array([1, 2, 3, 4, 5, 6])    
In [8]: a.indices            # csr has coor in indices and indptr
Out[8]: array([0, 2, 2, 0, 1, 2])
In [9]: a.indptr
Out[9]: array([0, 2, 3, 6])

In [10]: ac=a.tocoo()
In [11]: ac.data
Out[11]: array([1, 2, 3, 4, 5, 6])
In [12]: ac.col
Out[12]: array([0, 2, 2, 0, 1, 2])
In [13]: ac.row
Out[13]: array([0, 0, 1, 2, 2, 2])

These values are compatible with the ones you input, but aren't guaranteed to be the same. 这些值与您输入的值兼容,但不保证是相同的。

In [14]: a.nonzero()
Out[14]: (array([0, 0, 1, 2, 2, 2]), array([0, 2, 2, 0, 1, 2]))
In [17]: a[a.nonzero()].A
Out[17]: array([[1, 2, 3, 4, 5, 6]])

nonzero also returns the coor, by the same coo conversion, but first it cleans up the data (removing extra zeros, etc). nonzero也通过相同的coo转换返回coor,但首先它清理数据(删除额外的零等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM