【发布时间】:2019-07-19 18:26:37
【问题描述】:
我需要计算 np.array 中相同元素的索引的平均值
我已经尝试使用 np.where 函数进行映射和列表理解,但它们返回我需要转换回 np.like 的 python 列表。 并且很遗憾自己无法从 numpy 中找到合适的东西,而且对 numpy 不太了解
有一个我尝试做的例子
A = np.array ([2,5,9,8,8,3,2,1,2,1,8])
set_ = np.unique(A)
indeces = [np.where(A==i) for i in set_]
mean_ = [np.mean(i) for i in indeces]
但是列表理解给出了一个列表,而 np.where - ndarray 我想使用 numpy 而不进行不必要的转换
我尝试使用 map 和 np.fromiter 之类的:
indeces = map(np.where,[A==i for i in set_])
mean_ = np.fromiter(indeces,dtype = np.int)
但它提供: ValueError: 使用序列设置数组元素。
mean_ = [8.0, 4.666666666666667, 5.0, 1.0, 5.666666666666667, 2.0]
使用上面的代码,但是请任何人都可以提出一种有效的方法来使用 numpy 或最接近的方法来完成此操作。 感谢关注)
【问题讨论】:
-
您能否编辑您的问题以包含给定输入所需的输出?
-
您能举例说明输出或最终结果应该是什么样子吗?