我正在尝试创建一个函数来计算一组数据的移动平均值。这是功能:
def averaged_rel_track(navg, rel_values, nb, zeroindex):
#function to average the relative track values for each blade. This is
#dependant on the number values specified by the user to average over in a
#rolling average
for blade in range(0,int(nb)):
av_values=[]
rel_blade=rel_values[:,blade]
for rev in range(0,len(rel_blade)):
section=rel_blade[rev-int(navg)+1:rev]
av_value=np.mean(section)
av_values.append(av_value)
print av_values
但是我想对其添加一个检查,并且我正在努力实现这一点的最佳方法...如果navg
移动平均线中的平均数 ( ) 是 24,则取指定之前的 23 个值的平均值元素和元素。但是,如果这 24 个值之一为零,我希望该指定元素的平均值为零。我试过了:
def averaged_rel_track(navg, rel_values, nb, zeroindex):
#function to average the relative track values for each blade. This is
#dependant on the number values specified by the user to average over in a
#rolling average
for blade in range(0,int(nb)):
av_values=[]
rel_blade=rel_values[:,blade]
for rev in range(0,len(rel_blade)):
section=rel_blade[rev-int(navg)+1:rev]
av_value=np.mean(section)
zero_test= np.where(np.any(section==0))
print zero_test
if len(zero_test)==0:
av_value=None
av_values.append(av_value)
print av_values
然而 zero_test 的长度总是 1?
谁能想到一种方法来纠正这种方法或完全不同的方法?
干杯