1 Star 0 Fork 35

Rong Lin/Book6_First-Course-in-Data-Science

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Bk6_Ch06_03.py 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
Visualize-ML 提交于 2023-01-16 15:06 . Add files via upload
###############
# Authored by Weisheng Jiang
# Book 6 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
import statsmodels.api as sm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader
import scipy.stats as stats
df = pandas_datareader.data.DataReader(['UNRATENSA'], data_source='fred', start='08-01-2000', end='08-01-2021')
df = df.dropna()
# deal with missing values
df['UNRATENSA'].interpolate(inplace=True)
res = sm.tsa.seasonal_decompose(df['UNRATENSA'])
# generate subplots
resplot = res.plot()
res.resid
res.seasonal
res.trend
#%% Original data
fig, axs = plt.subplots()
df['UNRATENSA'].plot()
plt.xlabel('Date')
plt.ylabel('Original')
plt.show()
#%% plot trend on top of original curve
df['UNRATENSA'].plot()
res.trend.plot(color = 'r')
plt.xlabel('Date')
plt.ylabel('Trend')
plt.show()
#%% plot seasonal component
fig, axs = plt.subplots()
res.seasonal.plot()
plt.axhline(y = 0, color = 'r')
plt.xlabel('Date')
plt.ylabel('Seasonal')
plt.show()
#%% plot irregular
fig, axs = plt.subplots()
res.resid.plot()
plt.axhline(y = 0, color = 'r')
plt.xlabel('Date')
plt.ylabel('irregular')
plt.show()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/aaronlr/Book6_First-Course-in-Data-Science.git
git@gitee.com:aaronlr/Book6_First-Course-in-Data-Science.git
aaronlr
Book6_First-Course-in-Data-Science
Book6_First-Course-in-Data-Science
main

搜索帮助