gpt4 book ai didi

python - 在 Python 中从数组中提取特定值

转载 作者:太空宇宙 更新时间:2023-11-04 08:34:43 25 4
gpt4 key购买 nike

我想计算数据集中的均值和平均值,但出于多种原因我无法在此处详述,我的数组包含我的值和一些“填充”值(当前设置为 -1000)。

如何计算非-1000 值的平均值(例如)?

res=[-1000 for x in range(0,10)]
res[1]=2
res[5]=3
res[7]=4
#something like this?
np.mean(res>-1000)

#the result should be the mean value of 2,3 and 4 (3)

MVCE

res=[1.0, 1.0, 1.0, 1.0, 1.0, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000, -1000]
#for instance
print(np.mean(res[res > -1000]))

最佳答案

既然你标记了numpy,你应该用它来索引/切片。这是一个例子:

res = np.array([-1000 for x in range(0,10)])
res[1]=2
res[5]=3
res[7]=4

output = np.mean(res[res > -1000]) # 3.0

读取numpy docs有关索引逻辑的更多详细信息。

关于python - 在 Python 中从数组中提取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50137367/

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