This question already has answers here:
How to reduce execution time in algorithm by replacing for loop in python
                                
                                    (6个答案)
                                
                        
                                9个月前关闭。
            
                    
我想计算一个numpy数组的平均值,但要在从数组开始到该数组的实际值的窗口内计算平均值。这是一个例子:

array = [1, 2, 3, 4, 5, 6, ...]
windowed_means = [1, 1.5, 2, 2.5, ...]


演算就像:

windowed_means = [1/1, (1+2)/2, (1+2+3)/3, (1+2+3+4)/4 , ...]


有什么功能吗?

最佳答案

你可以试试:

windowed_means = np.cumsum(array)/np.arange(1,len(array)+1)


numpy.cumsumcumulative sum

关于python - 脾气暴躁的聚合平均值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55219355/

10-13 06:43