時間序列預(yù)測的7種Python工具包,總有一款適合你!
時間序列問題是數(shù)據(jù)科學(xué)中最難解決的問題之一。傳統(tǒng)的處理方法如 ARIMA、SARIMA 等,雖然是很好,但在處理具有非線性特性或非平穩(wěn)時間序列問題時很難取得滿意的預(yù)測效果。
為了獲得更好的預(yù)測效果,并且可以簡單高效的完成任務(wù),本文中我將分享給大家7個用于處理時間序列問題的 Python 工具包。
1、tsfresh

tsfresh 是一個很棒的 python 包,它可以自動計算大量的時間序列特性,包含許多特征提取方法和強大的特征選擇算法。
讓我們以獲取航空公司乘客的標(biāo)準(zhǔn)數(shù)據(jù)集為例,來了解tsfresh
# Importing libraries
import pandas as pd
from tsfresh import extract_features, extract_relevant_features, select_features
from tsfresh.utilities.dataframe_functions import impute, make_forecasting_frame
from tsfresh.feature_extraction import ComprehensiveFCParameters, settings
# Reading the data
data = pd.read_csv('../input/air-passengers/AirPassengers.csv')
# Some preprocessing for time component:
data.columns = ['month','Passengers']
data['month'] = pd.to_datetime(data['month'],infer_datetime_format=True,format='%y%m')
data.index = data.month
df_air = data.drop(['month'], axis = 1)
# Use Forecasting frame from tsfresh for rolling forecast training
df_shift, y_air = make_forecasting_frame(df_air["Passengers"], kind="Passengers", max_timeshift=12, rolling_direction=1)
print(df_shift)
數(shù)據(jù)需要被格式化為如下格式:

# 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)
從上面的輸出中,我們可以看到大約創(chuàng)建了800個特征。tsfresh還有助于基于p值的特征選擇。更多詳細(xì)信息可以查看Github:https://github.com/blue-yonder/tsfresh官方文檔 https://tsfresh.readthedocs.io/en/latest/index.html
2、autots

AutoTS 是一個自動化的時間序列預(yù)測庫,可以使用簡單的代碼訓(xùn)練多個時間序列模型,此庫的一些最佳功能包括:
利用遺傳規(guī)劃優(yōu)化方法尋找最優(yōu)時間序列預(yù)測模型。 提供置信區(qū)間預(yù)測值的下限和上限。 它訓(xùn)練各種各樣的模型,如統(tǒng)計的,機器學(xué)習(xí)以及深度學(xué)習(xí)模型 它還可以執(zhí)行最佳模型的自動集成 它還可以通過學(xué)習(xí)最優(yōu)NaN插補和異常值去除來處理混亂的數(shù)據(jù) 它可以運行單變量和多變量時間序列
讓我們以蘋果股票數(shù)據(jù)集為例,更詳細(xì)地了解一下:
# 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)
這將運行數(shù)百個模型。你將在輸出窗格中看到運行的各種模型。讓我們看看模型如何預(yù)測:
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()
更多詳細(xì)信息可以查看Github: https://github.com/winedarksea/AutoTS官網(wǎng)文檔: https://winedarksea.github.io/AutoTS/build/html/source/tutorial.html
3、Prophet
Prophet是Facebook研究團隊開發(fā)的知名時間序列軟件包,于2017年首次發(fā)布,適用于具有強烈季節(jié)性影響的數(shù)據(jù)和多個季節(jié)的歷史數(shù)據(jù)。它具有高度的用戶友好性和可定制性,只需進行最少的設(shè)置。
讓我們看一個簡單的例子:
# 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.
趨勢圖和季節(jié)性圖如下所示:
我們還可以看到預(yù)測以及所有的置信區(qū)間
更多詳細(xì)信息可以查看Github: https://github.com/facebook/prophet文檔: https://facebook.github.io/prophet/
4、darts:
Darts 是另一個 Python 包,它有助于時間序列的操作和預(yù)測。語法是“sklearn-friendly”,使用fit和predict函數(shù)來實現(xiàn)目標(biāo)。此外,它還包含了從 ARIMA 到神經(jīng)網(wǎng)絡(luò)的各種模型。
該軟件包最好的部分是它不僅支持單變量,而且還支持多變量時間序列和模型。該庫還可以方便地對模型進行回溯測試,并將多個模型的預(yù)測和外部回歸組合起來。讓我們舉一個簡單的例子來了解它的工作原理:
#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()
model.fit(train)
# Getting and plotting the predictions
prediction = model.predict(len(val))series.plot(label='actual')
prediction.plot(label='forecast', lw=3)
plt.legend()

更多詳細(xì)信息可以查看Github: https://github.com/unit8co/darts文檔: https://unit8co.github.io/darts/README.html
5、AtsPy
AtsPy 代表Python 中的自動時間序列模型。該庫的目標(biāo)是預(yù)測單變量時間序列。你可以加載數(shù)據(jù)并指定要運行的模型,如下例所示:
# Importing packages
import pandas as pd
from atspy import AutomatedModel
# Reading the data:
data = pd.read_csv('../input/air-passengers/AirPassengers.csv')
# Preprocessing data
data.columns = ['month','Passengers']
data['month'] = pd.to_datetime(data['month'],infer_datetime_format=True,format='%y%m')
data.index = data.month
df_air = data.drop(['month'], axis = 1)
# Select the models you want to run:
models = ['ARIMA','Prophet']
run_models = AutomatedModel(df = df_air, model_list=models, forecast_len=10)
該軟件包提供了一組完全自動化的不同模型。以下是可用型號的截圖:
Github: https://github.com/firmai/atspy
6、kats:
Kats 是 Facebook 研究團隊最近開發(fā)的另一個專門處理時間序列數(shù)據(jù)的庫。該框架的目標(biāo)是為解決時間序列問題提供一個完整的解決方案。使用此庫,我們可以執(zhí)行以下操作:
時間序列分析 模式檢測,包括季節(jié)性、異常值、趨勢變化 產(chǎn)生65個特征的特征工程模塊 對時間序列數(shù)據(jù)建立預(yù)測模型,包括Prophet、ARIMA、Holt Winters等。
它剛剛發(fā)布了第一個版本。一些教程可以在這里找到https://github.com/facebookresearch/Kats/tree/master/tutorials
7、sktime:
Sktime 庫是一個統(tǒng)一的 python 庫,它適用于時間序列數(shù)據(jù),并且與 scikit-learn 兼容。它有時間序列預(yù)測、回歸和分類模型,開發(fā)的主要目標(biāo)是與 scikit-learn 進行互操作。
舉個預(yù)測例子來介紹 sktime 的使用方法
from sktime.datasets import load_airline
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.forecasting.theta import ThetaForecaster
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
y = load_airline()
y_train, y_test = temporal_train_test_split(y)
fh = ForecastingHorizon(y_test.index, is_relative=False)
forecaster = ThetaForecaster(sp=12) # monthly seasonal periodicity
forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
mean_absolute_percentage_error(y_test, y_pred)
>>> 0.08661467738190656
Github:https://github.com/alan-turing-institute/sktime
資料分享
Python 程序員深度學(xué)習(xí)的“四大名著”:
這四本書內(nèi)容著實很不錯,非常適合學(xué)習(xí)機器學(xué)習(xí)、深度學(xué)習(xí)方向的小伙伴!我們都知道現(xiàn)在這方面的資料太多了,面對海量資源,往往陷入到“無從下手”的困惑出境,而且并非所有的書籍都是優(yōu)質(zhì)資源,浪費大量的時間是得不償失的。
獲得方式:
掃碼或者長按下方二維碼,點擊右上方關(guān)注; 后臺回復(fù)關(guān)鍵詞:4books;
