Python 和 Scipy 的季节性调整
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2067095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Seasonal adjustment in Python and Scipy
提问by Thomas Browne
I am looking to seasonally adjust monthly data, using Python. As you can see from these series: www.emconfidential.com, there is a high seasonal component to the data. I would like to adjust for this so that I can better guage if the series trend is rising or falling. Anybody know how to do this easily using scipy or other Python library?
我希望使用 Python 按季节调整每月数据。从这些系列中可以看出:www.emconfidential.com数据具有很高的季节性成分。我想对此进行调整,以便我可以更好地判断系列趋势是上升还是下降。有人知道如何使用 scipy 或其他 Python 库轻松做到这一点吗?
采纳答案by DisplacedAussie
There's no magical python library that will do seasonal adjustments for you. Applications that do this kind of thing tend to be rather large.
没有神奇的 Python 库可以为您进行季节性调整。做这种事情的应用程序往往相当大。
You'll need to work out the maths yourselfand then use scipy to calculate the rest for you.
您需要自己计算数学,然后使用 scipy 为您计算其余部分。
回答by user2113095
Statsmodels can do this. They have a basic seasonal decomposition and also a wrapper to Census X13 adjustment. You could also use rpy2 to access some of R's excellent SA libraries. Here is statsmodels seasonal decomp:
Statsmodels 可以做到这一点。他们有一个基本的季节性分解和一个人口普查 X13 调整的包装。您还可以使用 rpy2 访问一些 R 的优秀 SA 库。这是 statsmodels 季节性分解:
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
pd.options.display.mpl_style = 'default'
%matplotlib inline
dta = sm.datasets.co2.load_pandas().data.resample("M").fillna(method="ffill")
res = sm.tsa.seasonal_decompose(dta)
fig = res.plot()
fig.set_size_inches(10, 5)
plt.tight_layout()
http://statsmodels.sourceforge.net/0.6.0/release/version0.6.html
http://statsmodels.sourceforge.net/0.6.0/release/version0.6.html
回答by splinter
回答by binjip
I would suggest Prophetdeveloped by the data science team at Facebook. It has Python+R API and is used for time-series prediction although you can use it just for decomposing your series into its components (trend vs seasonality). You can easily adjust and visualize the decomposition:
我建议由 Facebook 数据科学团队开发的Prophet。它具有 Python+R API 并用于时间序列预测,尽管您可以仅将其用于将序列分解为其组成部分(趋势与季节性)。您可以轻松调整和可视化分解:
from fbprophet import Prophet
import numpy as np
import pandas as pd
# Create series
np.random.seed(0)
x = np.arange(0, 10, .285)
y_periodic = np.sin(x*np.pi)
y_random = np.random.normal(size=len(x))
y_trend = x / 10.
df = pd.DataFrame({'ds': pd.date_range('01-01-2017', periods=len(x)),
'y': y_periodic})
df.head() # has to be a DataFrame with columns "ds" and "y"
df.set_index('ds').plot(style='-*')
# Estimate the model
m = Prophet()
m.fit(df);
forecast = m.predict(df)
m.plot_components(forecast);
回答by ridecar2
Not sure on the programming aspect of this but I would seriously consider moving averages to solve this.
不确定这方面的编程方面,但我会认真考虑移动平均线来解决这个问题。