答案 0 :(得分:0)
您的数据或多或少为coo
格式,因此请使用scipy.sparse.coo_matrix
构造函数。所得的稀疏矩阵可以转换为多种格式。
示例:
>>> from pprint import pprint
>>> import numpy as np
>>> from scipy import sparse
>>>
# create example
>>> a = np.random.randint(-10, 4, (10, 10)).clip(0, None) * 0.24
>>> a
array([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0.24, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[0.72, 0.24, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. , 0.24, 0. , 0. , 0. , 0. ],
[0. , 0. , 0.24, 0. , 0.72, 0. , 0.48, 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.48, 0.24, 0. ],
[0. , 0. , 0. , 0.48, 0.48, 0. , 0. , 0.24, 0. , 0. ],
[0. , 0. , 0. , 0. , 0. , 0.24, 0.48, 0. , 0. , 0.24],
[0. , 0.48, 0. , 0. , 0. , 0. , 0.72, 0. , 0. , 0.72],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])
>>>
# construct coo representation
>>> row, col = np.where(a)
>>> coo = np.rec.fromarrays([row, col, a[row, col]], names='row col value'.split())
>>> pprint(coo.tolist())
[(1, 2, 0.24),
(2, 0, 0.72),
(2, 1, 0.24),
(3, 5, 0.24),
(4, 2, 0.24),
(4, 4, 0.72),
(4, 6, 0.48),
(5, 7, 0.48),
(5, 8, 0.24),
(6, 3, 0.48),
(6, 4, 0.48),
(6, 7, 0.24),
(7, 5, 0.24),
(7, 6, 0.48),
(7, 9, 0.24),
(8, 1, 0.48),
(8, 6, 0.72),
(8, 9, 0.72)]
>>>
# create sparse matrix
>>> out = sparse.coo_matrix((coo['value'], (coo['row'], coo['col'])), (10, 10))
>>> out
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 18 stored elements in COOrdinate format>
>>>
# convert back to dense format
>>> out.A
array([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0.24, 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[0.72, 0.24, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. , 0.24, 0. , 0. , 0. , 0. ],
[0. , 0. , 0.24, 0. , 0.72, 0. , 0.48, 0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.48, 0.24, 0. ],
[0. , 0. , 0. , 0.48, 0.48, 0. , 0. , 0.24, 0. , 0. ],
[0. , 0. , 0. , 0. , 0. , 0.24, 0.48, 0. , 0. , 0.24],
[0. , 0.48, 0. , 0. , 0. , 0. , 0.72, 0. , 0. , 0.72],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])