使用包含索引索引的字典来填充numpy阵列



我试图基于值字典来填充一个numpy数组。

我的字典看起来像这样:

a = {(12,15):4,(532,31):7,(742,1757):1,...}

我试图填充数组,以便(使用上述示例)4处于索引(12,15)等。A中的键称为" D"," S",值称为"计数"。

a = {(d,s):count}

目前我的代码填充数组看起来像这样:

N = len(seen)
Am = np.zeros((N,N), 'i')
for key, count in A.items():
    Am[d,s] = count

,但这只是导致多个数组,主要是创建的零。

谢谢

这是一种方法 -

def dict_to_arr(A):
    idx = np.array(list(A.keys()))
    val = np.array(list(A.values()))
    m,n = idx.max(0)+1 # max extents of indices to decide o/p array
    out = np.zeros((m,n), dtype=val.dtype)
    out[idx[:,0], idx[:,1]] = val # or out[tuple(idx.T)] = val
    return out

如果我们避免了索引和值的数组转换,并直接使用在最后一步分配的数组,例如 -

out[zip(*A.keys())] = list(A.values())

样本运行 -

In [3]: A = {(12, 15): 4, (532, 31): 7, (742, 1757): 1}
In [4]: arr = dict_to_arr(A)
In [5]: arr[12,15], arr[532,31], arr[742,1757]
Out[5]: (4, 7, 1)

存储到稀疏矩阵

为了节省内存并可能获得性能,我们可能要存储在稀疏的矩阵中。让我们用 csr_matrix做到这一点,就像这样 -

from scipy.sparse import csr_matrix
def dict_to_sparsemat(A):
    idx = np.array(list(A.keys()))
    val = np.array(list(A.values()))
    m,n = idx.max(0)+1
    return csr_matrix((val, (idx[:,0], idx[:,1])), shape=(m,n))

样本运行 -

In [64]: A = {(12, 15): 4, (532, 31): 7, (742, 1757): 1}
In [65]: out = dict_to_sparsemat(A)
In [66]: out[12,15], out[532,31], out[742,1757]
Out[66]: (4, 7, 1)

最新更新