我有一本词典,其条目标记为{(k,i): value, ...}
。我现在想把这个字典转换成一个二维数组,其中在位置[k,i]
处给出的数组元素的值是来自字典的值,其值为(k,i)
。行的长度将不一定是相同的大小(例如,行k = 4
可能上升到索引i = 60
,而行k = 24
可能上升到索引i = 31
)。由于不对称性,可以使特定行中的所有附加条目等于0以便具有矩形矩阵。将已知索引的字典转换为多维数组
4
A
回答
3
这里有一个方法 -
# Get keys (as indices for output) and values as arrays
idx = np.array(d.keys())
vals = np.array(d.values())
# Get dimensions of output array based on max extents of indices
dims = idx.max(0)+1
# Setup output array and assign values into it indexed by those indices
out = np.zeros(dims,dtype=vals.dtype)
out[idx[:,0],idx[:,1]] = vals
我们也可以使用稀疏矩阵得到最终的输出。例如与coordinate format sparse matrices
。当保存为稀疏矩阵时,这将是高效的内存。所以,最后可能的东西来代替这样的 -
from scipy.sparse import coo_matrix
out = coo_matrix((vals, (idx[:,0], idx[:,1])), dims).toarray()
采样运行 -
In [70]: d
Out[70]: {(1, 4): 120, (2, 2): 72, (2, 3): 100, (5, 2): 88}
In [71]: out
Out[71]:
array([[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 120],
[ 0, 0, 72, 100, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 88, 0, 0]])
为了使通用的任意维数的ndarrays,我们可以用线性 - 索引并使用np.put
将值分配到输出数组中。因此,在我们的第一途径,只需更换的是这样分配值的最后一步 -
np.put(out,np.ravel_multi_index(idx.T,dims),vals)
采样运行 -
In [106]: d
Out[106]: {(1,0,0): 99, (1,0,4): 120, (2,0,2): 72, (2,1,3): 100, (3,0,2): 88}
In [107]: out
Out[107]:
array([[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]],
[[ 99, 0, 0, 0, 120],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 72, 0, 0],
[ 0, 0, 0, 100, 0]],
[[ 0, 0, 88, 0, 0],
[ 0, 0, 0, 0, 0]]])
0
有一个字典的密钥稀疏格式,可从这样的字典中建立。
与Divakar's
d
样品开始:
In [1189]: d={(1, 4): 120, (2, 2): 72, (2, 3): 100, (5, 2): 88}
做出正确的形状和D型的空稀疏矩阵:
In [1190]: M=sparse.dok_matrix((6,5),dtype=int)
In [1191]: M
Out[1191]:
<6x5 sparse matrix of type '<class 'numpy.int32'>'
with 0 stored elements in Dictionary Of Keys format>
经由字典update
添加d
值。这是有效的,因为这个特殊的稀疏格式是dict
的子类。要洁具虽然这招未记录(至少不是我所知道的):
In [1192]: M.update(d)
In [1193]: M
Out[1193]:
<6x5 sparse matrix of type '<class 'numpy.int32'>'
with 4 stored elements in Dictionary Of Keys format>
In [1194]: M.A # convert M to numpy array (handy display trick)
Out[1194]:
array([[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 120],
[ 0, 0, 72, 100, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 88, 0, 0]])
M
可以转换成其他格式的稀疏,coo
,csr
。实际上sparse
根据用途(显示,计算等)自行进行这种转换。
In [1196]: print(M)
(2, 3) 100
(5, 2) 88
(1, 4) 120
(2, 2) 72
相关问题
- 1. 将一维数组索引转换为二维数组索引
- 2. 将二维数组索引转换为一维索引
- 3. 将一维数组的索引转换为二维数组
- 4. 转换索引多维数组关联多维数组
- 5. 将数组转换为多维数组?
- 6. 将多维数组列表转换为单个多维数组?
- 7. 将字符串转换多维数组
- 8. 如何在经典ASP中将多维数组转换为JSON?
- 9. 将多维数组转换为一维数组的算法
- 10. 将二维数组转换为二维数组的多个块
- 11. 字符串转换为多维数组
- 12. 将多维数组转换为对象
- 13. 将HTML DOM转换为多维数组
- 14. 将现有数组转换为多维
- 15. 将JObject转换为多维数组
- 16. 将Fortran多维数组转换为C
- 17. 如何将二维数组转换为字典对象
- 18. 多维数组将数组转换为字符串
- 19. 将表格字段值转换为数组或多维数组
- 20. 将一维索引转换为二维索引
- 21. 数组转换为字典
- 22. 在MATLAB中将子数组索引转换为数组索引?
- 23. 将单维数组转换为多维数组
- 24. 将一维数组转换为多维数组
- 25. 如何将多维数组转换为单维数组?
- 26. 将多维数组转换为一维数组
- 27. 将多维数组转换为单维关联jquery数组
- 28. 如何将多维数组转换为一维数组?
- 29. 将单个二维数组转换为多维数组
- 30. 将位索引数组转换为OptionSet
样品输入/输出? –