简体   繁体   English

滚动意味着窗口增加

[英]rolling mean with increasing window

I have a range 我有一个范围

np.arange(1,11) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

and for each element, i , in my range I want to compute the average from element i=0 to my current element. 对于每个元素, ,在我的范围内,我想计算从元素i = 0到我当前元素的平均值。 the result would be something like: 结果将是这样的:

array([ 1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ,  5.5])
 # got this result via np.cumsum(np.arange(1,11,dtype=np.float32))/(np.arange(1, 11))

I was wondering if there isn't an out of the box function in numpy / pandas that gives me this result? 我想知道numpy / pandas中是否没有开箱即用功能给我这个结果?

You can use expanding() (requires pandas 0.18.0): 你可以使用expanding() (需要pandas 0.18.0):

ser = pd.Series(np.arange(1, 11))

ser.expanding().mean()
Out: 
0    1.0
1    1.5
2    2.0
3    2.5
4    3.0
5    3.5
6    4.0
7    4.5
8    5.0
9    5.5
dtype: float64

This seems to be the simplest, although it may become inefficient if x is very large: 这似乎是最简单的,但如果x非常大,它可能会变得低效:

x = range(1,11)
[np.mean(x[:i+1]) for i in xrange(0,len(x))]

Here's a vectorized approach - 这是一个矢量化的方法 -

a.cumsum()/(np.arange(a.size)+1)

Please note that to make sure the results are floating pt numbers, we need to add in at the start : 请注意,为了确保结果是浮点数,我们需要在开头添加:

from __future__ import division

Alternatively, we can use np.true_divide for the division - 或者,我们可以使用np.true_divide进行划分 -

np.true_divide(a.cumsum(),(np.arange(a.size)+1))

Sample runs - 样品运行 -

In [17]: a
Out[17]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

In [18]: a.cumsum()/(np.arange(a.size)+1)
Out[18]: array([ 1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5,  5. ,  5.5])

In [20]: a
Out[20]: array([3, 3, 2, 4, 6, 6, 3, 5, 6, 4])

In [21]: a.cumsum()/(np.arange(a.size)+1)
Out[21]: 
array([ 3.        ,  3.        ,  2.66666667,  3.        ,  3.6       ,
        4.        ,  3.85714286,  4.        ,  4.22222222,  4.2       ])

From Pandas 0.18.0 out of the box, as you wanted :) 从Pandas 0.18.0开箱即用,如你所愿:)

s = pd.Series([1, 2, 3, 4, 5])
s.rolling(5, min_periods=1).mean()

result is: 结果是:

0    1.0
1    1.5
2    2.0
3    2.5
4    3.0
dtype: float64

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

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