df_shift, y_air = make_forecasting_frame(df_air[“Passengers”], kind=“Passengers”, max_timeshift=12, rolling_direction=1)
print(df_shift)
数据需要被格式化为如下格式:
Getting Comprehensive Features
extraction_settings = ComprehensiveFCParameters()
X = extract_features(df_shift, column_id=“id”, column_sort=“time”, column_value=“value”, impute_function=impute,show_warnings=False,default_fc_parameters=extraction_settings)
从上面的输出中,我们可以看到大约创建了800个特征。tsfresh还有助于基于p值的特征选择。更多详细信息可以查看Github: https://github.com/blue-yonder/tsfresh
官方文档 https://tsfresh.readthedocs.io/en/latest/index.html
2、autots
AutoTS 是一个自动化的时间序列预测库,可以使用简单的代码训练多个时间序列模型,此库的一些最佳功能包括:
-
利用遗传规划优化方法寻找最优时间序列预测模型。
-
提供置信区间预测值的下限和上限。
-
它训练各种各样的模型,如统计的,机器学习以及深度学习模型
-
它还可以执行最佳模型的自动集成
-
它还可以通过学习最优NaN插补和异常值去除来处理混乱的数据
-
它可以运行单变量和多变量时间序列
让我们以苹果股票数据集为例,更详细地了解一下:
Loading the package
from autots import AutoTS
import matplotlib.pyplot as plt
import pandas as pd
Reading the data
df = pd.read_csv(‘…/input/apple-aapl-historical-stock-data/HistoricalQuotes.csv’)
Doing some preprocessing
def remove_dollar(x):
return x[2:]
df[’ Close/Last’] = df[’ Close/Last’].apply(remove_dollar)
df[’ Close/Last’] = df[’ Close/Last’].astype(float)
df[‘Date’] = pd.to_datetime(df[‘Date’])
Plot to see the data:
df = df[[“Date”, " Close/Last"]]
df[“Date”] = pd.to_datetime(df.Date)
temp_df = df.set_index(‘Date’)
temp_df[" Close/Last"].plot(figsize=(12, 8), title=“Apple Stock Prices”, fontsize=20, label=“Close Price”)
plt.legend()
plt.grid()
plt.show()
model = AutoTS(forecast_length=40, frequency=‘infer’, ensemble=‘simple’, drop_data_older_than_periods=100)
model = model.fit(df, date_col=‘Date’, value_col=’ Close/Last’, id_col=None)
这将运行数百个模型。你将在输出窗格中看到运行的各种模型。让我们看看模型如何预测:
prediction = model.predict()
forecast = prediction.forecast
print(“Stock Price Prediction of Apple”)
print(forecast)
temp_df[’ Close/Last’].plot(figsize=(15,8), title= ‘AAPL Stock Price’, fontsize=18, label=‘Train’)
forecast[’ Close/Last’].plot(figsize=(15,8), title= ‘AAPL Stock Price’, fontsize=18, label=‘Test’)
plt.legend()
plt.grid()
plt.show()
更多详细信息可以查看 Github: https://github.com/winedarksea/AutoTS
官网文档: https://winedarksea.github.io/AutoTS/build/html/source/tutorial.html
3、Prophet
Prophet是Facebook研究团队开发的知名时间序列软件包,于2017年首次发布,适用于具有强烈季节性影响的数据和多个季节的历史数据。它具有高度的用户友好性和可定制性,只需进行最少的设置。
让我们看一个简单的例子:
Loading the library
import pandas as pd
import matplotlib.pyplot as plt
from fbprophet import Prophet
Loading the data from the repo:
df = pd.read_csv(“https://raw.githubusercontent.com/facebook/prophet/master/examples/example_wp_log_peyton_manning.csv”)
Fitting the model
model = Prophet()
model.fit(df) #fit the model.
Predict
future = model.make_future_dataframe(periods=730) # predicting for ~ 2 years
forecast = model.predict(future) # Predict future
Plot results
fig1 = model.plot(forecast) # Plot the fit to past data and future forcast.
fig2 = model.plot_components(forecast) # Plot breakdown of components.
plt.show()
forecast # Displaying various results in table format.
趋势图和季节性图如下所示:
我们还可以看到预测以及所有的置信区间
更多详细信息可以查看Github: https://github.com/facebook/prophet
文档: https://facebook.github.io/prophet/
4、darts:
Darts 是另一个 Python 包,它有助于时间序列的操作和预测。语法是“sklearn-friendly”,使用fit和predict函数来实现目标。此外,它还包含了从 ARIMA 到神经网络的各种模型。
该软件包最好的部分是它不仅支持单变量,而且还支持多变量时间序列和模型。该库还可以方便地对模型进行回溯测试,并将多个模型的预测和外部回归组合起来。让我们举一个简单的例子来了解它的工作原理:
#Loading the package
from darts import TimeSeries
from darts.models import ExponentialSmoothing
import matplotlib.pyplot as plt
Reading the data
data = pd.read_csv(‘…/input/air-passengers/AirPassengers.csv’)
series = TimeSeries.from_dataframe(data, ‘Month’, ‘#Passengers’)
print(series)
Splitting the series in train and validation set
train, val = series.split_before(pd.Timestamp(‘19580101’))
Applying a simple Exponential Smoothing model
model = ExponentialSmoothing()
感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:
① 2000多本Python电子书(主流和经典的书籍应该都有了)
② Python标准库资料(最全中文版)
③ 项目源码(四五十个有趣且经典的练手项目及源码)
④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)
⑤ Python学习路线图(告别不入流的学习)
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!