簡體   English   中英

Pandas 的初始值 ewm mean

[英]Initial value for Pandas ewm mean

使用 pandas ewm.mean 計算指數加權移動平均值時,有沒有辦法設置初始值?

pandas ewm.mean 在系列的第一個值處初始化 - 我可以設置/覆蓋初始值嗎?

見代碼示例:

import pandas as pd
s = pd.Series([5, 1, 2, 4, 3])
alpha = 0.2

# computing the exponential mean using pandas ewm.mean:
a = s.ewm(alpha=alpha, adjust=False).mean()

# iterative solution with the same result:
b = pd.Series()
z = 5  # initial value
for i, x in s.items():
    z = alpha * x + (1 - alpha) * z
    b.loc[i] = z

# behaviour I want, i.e. initialize at a specific value:
c = pd.Series()
z = 3  # initial value
for i, x in s.items():
    z = alpha * x + (1 - alpha) * z
    c.loc[i] = z

print(pd.concat([s, a, b, c], axis=1, keys=['unsmoothed', 'pandas', 'iterative', 'initialized (the result I want)']))

替換第一個值實際上會給您帶來不同的結果。 要獲得您想要的結果,請預先添加初始值(例如,使用pd.concat ),然后在末尾iloc[1:]

z = 3  # initial value
pd.concat([pd.Series([z]), s]).ewm(alpha=alpha, adjust=False).mean().iloc[1:]

# 0    3.40000
# 1    2.92000
# 2    2.73600
# 3    2.98880
# 4    2.99104
# dtype: float64

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM