[英]Recency weighted moving average on previous dates in pandas
我有以下df:
index = pd.to_datetime(['2017-03-01', '2017-03-01', '2017-02-15', '2017-02-01',
'2017-01-20', '2017-01-20', '2017-01-20', '2017-01-02',
'2016-12-04', '2016-12-04', '2016-12-04', '2016-11-16'])
df = pd.DataFrame(data = {'val': [8, 1, 5, 2, 3 , 5, 9, 14, 13, 2, 1, 12],
'group': ['one', 'two', 'one', 'one', 'two', 'two', 'one', 'two',
'two', 'one', 'one', 'two']},
index=index)
df = df.sort_index()
group val
2016-11-16 two 12
2016-12-04 two 13
2016-12-04 one 2
2016-12-04 one 1
2017-01-02 two 14
2017-01-20 two 3
2017-01-20 two 5
2017-01-20 one 9
2017-02-01 one 2
2017-02-15 one 5
2017-03-01 one 8
2017-03-01 two 1
在每組(一,兩個)中,我希望最近一次加權的新近度加權平均值。 因此,例如查看第一組:
group val
2016-12-04 one 2
2016-12-04 one 1
2017-01-20 one 9
2017-02-01 one 2
2017-02-15 one 5
2017-03-01 one 8
例如,對於日期2017-02-15
,我希望計算一個新列,該列具有以前值[2,9]的新近度加權版本(對於過去的較近日期,權重較高)作為該日期的值,1,2]。 請注意,一組中可能有多次約會,而這些約會的權重應該相同。
我認為大熊貓的指數加權函數將對此有所幫助。 我認為一組中的日期是相同的,因此我將首先取這些值的平均值,以便以后可以應用簡單的shift()。 我嘗試了以下方法:
df = df.reset_index().set_index(['index', 'group']).groupby(
level=[0,1]).mean().reset_index().set_index('index')
現在,如果我對新近度加權不感興趣,可以嘗試類似
df = df.groupby('group')['val'].expanding().mean().groupby(level=0).shift()
然后與原始日期和組合並。 但是,當我嘗試使用pandas.ewma時,我缺少以下信息:
df.groupby('group')['val'].ewm(span=27).groupby(level=0).shift()
我可以遍歷各組:
grouped = df.groupby('group')['val']
for key, group in grouped:
print pd.ewma(group, span=27).shift()
index
2016-12-04 NaN
2017-01-20 1.500000
2017-02-01 5.388889
2017-02-15 4.174589
2017-03-01 4.404414
Name: val, dtype: float64
index
2016-11-16 NaN
2016-12-04 12.000000
2017-01-02 12.518519
2017-01-20 13.049360
2017-03-01 10.529680
然后以某種方式將組和日期與原始df
合並,但這似乎過於復雜。 有一個更好的方法嗎?
要執行新近度加權移動平均值而無需遍歷組並重新合並,可以使用apply
。
def rwma(group):
# perform the ewma
kwargs = dict(ignore_na=False, span=27, min_periods=0, adjust=True)
result = group.ewm(**kwargs).mean().shift().reset_index()
# rename the result column so that the merge goes smoothly
result.rename(columns={result.columns[-1]: 'rwma'}, inplace=True)
return result
recency = df.groupby('group')['val'].apply(rwma)
測試代碼:
import pandas as pd
df = pd.DataFrame(data={
'val': [8, 1, 5, 2, 3, 5, 9, 14, 13, 2, 1, 12],
'group': ['one', 'two', 'one', 'one', 'two', 'two',
'one', 'two', 'two', 'one', 'one', 'two']},
index=pd.to_datetime([
'2017-03-01', '2017-03-01', '2017-02-15', '2017-02-01',
'2017-01-20', '2017-01-20', '2017-01-20', '2017-01-02',
'2016-12-04', '2016-12-04', '2016-12-04', '2016-11-16'])
).sort_index()
recency = df.groupby('group')['val'].apply(rwma)
print(recency)
結果:
index rwma
group
one 0 2016-12-04 NaN
1 2016-12-04 2.000000
2 2017-01-20 1.481481
3 2017-02-01 4.175503
4 2017-02-15 3.569762
5 2017-03-01 3.899694
two 0 2016-11-16 NaN
1 2016-12-04 12.000000
2 2017-01-02 12.518519
3 2017-01-20 13.049360
4 2017-01-20 10.251243
5 2017-03-01 9.039866
根據斯蒂芬的詢問,這是一個工作版本:
def rwma(group):
# perform the ewma
kwargs = dict(ignore_na=False, span=27, min_periods=0, adjust=True)
result = group.resample('1D').mean().ewm(**kwargs).mean().shift()
result = result[group.index].reset_index()
# rename the result column so that the merge goes smoothly
result.rename(columns={result.columns[-1]: 'rwma'}, inplace=True)
return result
recency = df.groupby('group')['val'].apply(rwma)
print(recency)
輸出:
index rwma
group
one 0 2016-12-04 NaN
1 2016-12-04 NaN
2 2017-01-20 1.500000
3 2017-02-01 8.776518
4 2017-02-15 4.016278
5 2017-03-01 4.670166
two 0 2016-11-16 NaN
1 2016-12-04 12.000000
2 2017-01-02 12.791492
3 2017-01-20 13.844843
4 2017-01-20 13.844843
5 2017-03-01 6.284914
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.