2016-08-02 79 views
4

我有一本词典,其条目标记为{(k,i): value, ...}。我现在想把这个字典转换成一个二维数组,其中在位置[k,i]处给出的数组元素的值是来自字典的值,其值为(k,i)。行的长度将不一定是相同的大小(例如,行k = 4可能上升到索引i = 60,而行k = 24可能上升到索引i = 31)。由于不对称性,可以使特定行中的所有附加条目等于0以便具有矩形矩阵。将已知索引的字典转换为多维数组

+0

样品输入/输出? –

回答

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'sd样品开始:

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可以转换成其他格式的稀疏,coocsr。实际上sparse根据用途(显示,计算等)自行进行这种转换。

In [1196]: print(M) 
    (2, 3) 100 
    (5, 2) 88 
    (1, 4) 120 
    (2, 2) 72