如果我有一个数组
[0,0,0,0,0,0,1,1,2,2,2,2]
如何使用 numpy.mean()
计算数组中每个唯一值的平均值。
我希望 [6/12, 2/12,4/12]
不使用 count 或 len,而仅使用 np.mean
我刚刚开始使用 Python。
最佳答案
How can I compute the mean by each unique value in the array using
numpy.mean()
.
使用numpy.mean
可能是不可能的。
但要实现这一点,您可以使用 numpy.bincount
和 len
import numpy as np
np.bincount(a)/len(a)
array([0.5 , 0.16666667, 0.33333333])
关于python - 通过Python中的唯一值计算数组组的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63555186/