編輯: 似乎從scikits.timeseries.lib.moving_funcs子模塊mov_average_expw()
功能從SciKits(附加補充SciPy工具包)更適合您的問題的措辭。
要與平滑因子alpha
計算數據的exponential smoothing(這是維基百科的條款(1 - alpha)
):
>>> alpha = 0.5
>>> assert 0 < alpha <= 1.0
>>> av = sum(alpha**n.days * iq
... for n, iq in map(lambda (day, iq), today=max(days): (today-day, iq),
... sorted(zip(days, IQ), key=lambda p: p[0], reverse=True)))
95.0
以上是不漂亮,所以讓我們重構了一點:
from collections import namedtuple
from operator import itemgetter
def smooth(iq_data, alpha=1, today=None):
"""Perform exponential smoothing with factor `alpha`.
Time period is a day.
Each time period the value of `iq` drops `alpha` times.
The most recent data is the most valuable one.
"""
assert 0 < alpha <= 1
if alpha == 1: # no smoothing
return sum(map(itemgetter(1), iq_data))
if today is None:
today = max(map(itemgetter(0), iq_data))
return sum(alpha**((today - date).days) * iq for date, iq in iq_data)
IQData = namedtuple("IQData", "date iq")
if __name__ == "__main__":
from datetime import date
days = [date(2008,1,1), date(2008,1,2), date(2008,1,7)]
IQ = [110, 105, 90]
iqdata = list(map(IQData, days, IQ))
print("\n".join(map(str, iqdata)))
print(smooth(iqdata, alpha=0.5))
實施例:
$ python26 smooth.py
IQData(date=datetime.date(2008, 1, 1), iq=110)
IQData(date=datetime.date(2008, 1, 2), iq=105)
IQData(date=datetime.date(2008, 1, 7), iq=90)
95.0
平均值實際上不是在圖書館,因爲它是如此簡單:SUM(IQ)/ LEN(IQ)給出了智商的算術平均值。 – Kiv 2009-01-28 18:19:50
簡單的平均值...很簡單。但是更復雜的算法可能在標準庫中很有用。 – Jim 2009-01-28 18:26:58
numpy和scipy有大量的統計函數,包括平均值:) – Ryan 2009-01-28 18:28:30