我想计算沿轴的修剪平均值,而不需要显式循环。所以它应该做同样的事情:
arr=np.random.randn(3,10000)
arr[:,:5]+=999
arr=np.arange(3)[:,None]+arr
out=np.zeros(arr.shape[0])
for i in range(arr.shape[0]):
col=arr[i,:]
m=np.abs(col-col.mean())<2*col.std()
out[i]=col[m].mean()
print out
print arr.mean(1)
结果是:
[-0.01085947 0.99187648 1.98009922]
[ 0.48822938 1.49126719 2.47951973]