使用scipy时,我能够以以下格式转换数据:

(row, col)  (weight)
(0, 0)      5
(0, 47)     5
(0, 144)    5
(0, 253)    4
(0, 513)    5
...
(6039, 3107)    5
(6039, 3115)    3
(6039, 3130)    4
(6039, 3132)    2

如何将其转换为一个数组或稀疏矩阵,如果缺少权重值,则为零?(根据上述数据,第1至46列应填入零,以此类推…)
0 1 2 3  ... 47 48 49 50
1 [0 0 0 0 ... 5 0 0 0 0
2  2 0 1 0 ... 4 0 5 0 0
3  3 1 0 5 ... 1 0 0 4 2
4  0 0 0 4 ... 5 0 1 3 0
5  5 1 5 4 ... 0 0 3 0 1]

我知道在内存方面保持上面的格式更好,但是我需要它作为实验的矩阵。

最佳答案

scipy.sparse为你做。

import numpy as np
from scipy.sparse import dok_matrix

your_data = [((2, 7), 1)]

XDIM, YDIM = 10, 10 # Replace with your values

dct = {}
for (row, col), weight in your_data:
    dct[(row, col)] = weight

smat = dok_matrix((XDIM, YDIM))
smat.update(dct)

dense = smat.toarray()

print dense

'''
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
'''

08-17 00:12