Python 和 Scipy 中的季节调整

发布于 2024-08-18 06:18:38 字数 221 浏览 6 评论 0原文

我希望使用 Python 季节性调整每月数据。从这些系列中可以看出:www.emconfidential.com,季节性很高数据的组成部分。我想对此进行调整,以便我可以更好地判断系列趋势是上升还是下降。有人知道如何使用 scipy 或其他 Python 库轻松做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你另情深 2024-08-25 06:18:38

统计模型可以做到这一点。它们具有基本的季节性分解以及 Census 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

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:

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

掩耳倾听 2024-08-25 06:18:38

现在有一个软件包似乎正是您正在寻找的!查看 seasonal 包,这里是链接 。我个人觉得非常有用,想知道其他人的想法。

There is now a package that seems to be exactly what you are looking for! Check out the seasonal package, here is the link. I personally found it to be very useful, wondering what others think.

椵侞 2024-08-25 06:18:38

没有神奇的 Python 库可以为你进行季节性调整。执行此类操作的应用程序往往相当大

您需要自己计算出数学,然后使用 scipy 计算其余部分为你。

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.

You'll need to work out the maths yourself and then use scipy to calculate the rest for you.

旧故 2024-08-25 06:18:38

我建议使用 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);

趋势和季节性分解

I would suggest Prophet developed 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:

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='-*')

Series with noise

# Estimate the model
m = Prophet()
m.fit(df);
forecast = m.predict(df)
m.plot_components(forecast);

Trend and seasonality decomposition

雨轻弹 2024-08-25 06:18:38

不确定这方面的编程,但我会认真考虑移动平均线来解决这个问题。

Not sure on the programming aspect of this but I would seriously consider moving averages to solve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文