按值对矩阵进行分组和汇总

时间:2016-04-01 11:00:13

标签: python numpy

我有两个矩阵,probtotalHigh形状axbxcxdab是坐标。这是两个样本:

In [77]: prob[1,1,:]
Out[77]: 
array([[ 0.09,  0.01,  0.  ,  0.  ,  0.  ],
       [ 0.81,  0.09,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ]])

In [78]: totalHigh[1,1,:]
Out[78]: 
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6]])
不幸的是,

totalHigh包含有关结果的信息。相应地,prob包含这些结果的概率。例如,坐标1处的结果1,1的总概率为0.01+0.81

如何删除多余的维度?

预期结果

simplifiedHigh[1,1,:]
array([0, 1, 2, 3, 4, 5, 6])
simplifiedProb[1,1,:]
array([0.09, 0.82, 0.09, 0, 0, 0, 0])

如何以最有效的方式获得

1 个答案:

答案 0 :(得分:4)

您可以使用np.bincountnp.unique -

IDs = np.unique(totalHigh_sliced)
counts = np.bincount(totalHigh_sliced.ravel(),prob_sliced.ravel())

示例运行 -

In [215]: prob_sliced
Out[215]: 
array([[ 0.09,  0.01,  0.  ,  0.  ,  0.  ],
       [ 0.81,  0.09,  0.  ,  0.  ,  0.  ],
       [ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ]])

In [216]: totalHigh_sliced
Out[216]: 
array([[0, 1, 2, 3, 4],
       [1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6]])

In [217]: IDs = np.unique(totalHigh_sliced)
     ...: counts = np.bincount(totalHigh_sliced.ravel(),prob_sliced.ravel())
     ...: 

In [218]: IDs
Out[218]: array([0, 1, 2, 3, 4, 5, 6])

In [219]: counts
Out[219]: array([ 0.09,  0.82,  0.09,  0.  ,  0.  ,  0.  ,  0.  ])