简体   繁体   English

如何计算numpy的多个平均值?

[英]How to calculate multiple average in numpy?

In the case, 在这种情况下,

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,1,100)
y=x**2

result=np.average(y)
print(result)

I need to calculate two sets of average as 我需要计算两组平均值

if y>.5 :
    result1=np.average(y=.5)
    result2.np.average(y-.5)
if y<=.5
    result1=np.average(y)

result1 has a maximum y value, and result2 a minimum. result1具有最大的y值, result2具有最小的y值。 However, I cannot apply the if condition for a numpy array. 但是,我不能将numpy数组的if条件应用于。

You can determine which elements are greater or less than 0.5 , then use that to index back into y 您可以确定哪些元素大于或小于0.5 ,然后使用该元素索引回y

>>> np.average(y[y>0.5])
0.7443118049178656
>>> np.average(y[y<0.5])
0.16784001632486484

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM