有沒有一種更有效的方法來獲取預先指定的數組中的平均數組?例如,我有一個數組數組和一個數組,對應於該數組中的bin開始和結束位置,並且我想僅在這些數組中使用平均值?我有下面的代碼,但我想知道如何削減和改進。謝謝。在python中使用scipy/numpy合併數據
from scipy import *
from numpy import *
def get_bin_mean(a, b_start, b_end):
ind_upper = nonzero(a >= b_start)[0]
a_upper = a[ind_upper]
a_range = a_upper[nonzero(a_upper < b_end)[0]]
mean_val = mean(a_range)
return mean_val
data = rand(100)
bins = linspace(0, 1, 10)
binned_data = []
n = 0
for n in range(0, len(bins)-1):
b_start = bins[n]
b_end = bins[n+1]
binned_data.append(get_bin_mean(data, b_start, b_end))
print binned_data
我沒有看到差異 - 哪個更快? – user248237dfsf 2011-05-28 22:24:11
@user:我不知道哪一個數據和參數更快。這兩種方法都應該比你的方法更快,我希望'histogram()'方法對於大量的bin來說更快。但是你必須自我介紹,我不能爲你做這件事。 – 2011-05-28 22:32:33