gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:02:26 25 4
gpt4 key购买 nike

我有两个矩阵,probtotalHigh 都是 axbxcxd 形状。 ab 是坐标。以下是两个示例:

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])

我如何以最有效的方式得到它?

最佳答案

您可以使用 np.bincountnp.unique -

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

sample 运行-

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. ])

关于python - 按值对矩阵进行分组和汇总,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36354626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com