我有两个矩阵,prob
和totalHigh
形状axbxcxd
。 a
和b
是坐标。这是两个样本:
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])
如何以最有效的方式获得 ?
答案 0 :(得分:4)
您可以使用np.bincount
和np.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. ])