日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
用XGBoost進行時間序列預(yù)測

XGBoost是梯度分類和回歸問題的有效實現(xiàn)。

成都創(chuàng)新互聯(lián)公司成立十多年來,這條路我們正越走越好,積累了技術(shù)與客戶資源,形成了良好的口碑。為客戶提供網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補等服務(wù)。網(wǎng)站是否美觀、功能強大、用戶體驗好、性價比高、打開快等等,這些對于網(wǎng)站建設(shè)都非常重要,成都創(chuàng)新互聯(lián)公司通過對建站技術(shù)性的掌握、對創(chuàng)意設(shè)計的研究為客戶提供一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。

它既快速又高效,即使在各種預(yù)測建模任務(wù)上也表現(xiàn)出色,即使不是最好的,也能在數(shù)據(jù)科學(xué)競賽的獲勝者(例如Kaggle的獲獎?wù)撸┲袕V受青睞。

XGBoost也可以用于時間序列預(yù)測,盡管它要求將時間序列數(shù)據(jù)集首先轉(zhuǎn)換為有監(jiān)督的學(xué)習(xí)問題。它還需要使用一種專門的技術(shù)來評估模型,稱為前向驗證,因為使用k倍交叉驗證對模型進行評估會導(dǎo)致樂觀的結(jié)果。

在本教程中,您將發(fā)現(xiàn)如何開發(fā)XGBoost模型進行時間序列預(yù)測。完成本教程后,您將知道:

1、XGBoost是用于分類和回歸的梯度提升集成算法的實現(xiàn)。

2、可以使用滑動窗口表示將時間序列數(shù)據(jù)集轉(zhuǎn)換為監(jiān)督學(xué)習(xí)。

3、如何使用XGBoost模型擬合,評估和進行預(yù)測,以進行時間序列預(yù)測。

教程概述

本教程分為三個部分:他們是:

1、XGBoost集成

2、時間序列數(shù)據(jù)準(zhǔn)備

3、XGBoost用于時間序列預(yù)測

XGBoost集成

XGBoost是Extreme Gradient Boosting的縮寫,是隨機梯度提升機器學(xué)習(xí)算法的有效實現(xiàn)。隨機梯度增強算法(也稱為梯度增強機或樹增強)是一種功能強大的機器學(xué)習(xí)技術(shù),可在各種具有挑戰(zhàn)性的機器學(xué)習(xí)問題上表現(xiàn)出色,甚至表現(xiàn)最佳。

它是決策樹算法的集合,其中新樹修復(fù)了那些已經(jīng)屬于模型的樹的錯誤。將添加樹,直到無法對模型進行進一步的改進為止。XGBoost提供了隨機梯度提升算法的高效實現(xiàn),并提供了一組模型超參數(shù),這些參數(shù)旨在提供對模型訓(xùn)練過程的控制。

XGBoost設(shè)計用于表格數(shù)據(jù)集的分類和回歸,盡管它可以用于時間序列預(yù)測。

首先,必須安裝XGBoost庫。您可以使用pip進行安裝,如下所示:

 
 
 
 
  1. sudo pip install xgboost 

一旦安裝,您可以通過運行以下代碼來確認它已成功安裝,并且您正在使用現(xiàn)代版本:

 
 
 
 
  1. # xgboost  
  2. import xgboost  
  3. print("xgboost", xgboost.__version__) 

運行代碼,您應(yīng)該看到以下版本號或更高版本。

 
 
 
 
  1. xgboost 1.0.1 

盡管XGBoost庫具有自己的Python API,但我們可以通過XGBRegressor包裝器類將XGBoost模型與scikit-learn API結(jié)合使用。

可以實例化模型的實例,就像將其用于模型評估的任何其他scikit-learn類一樣使用。例如:

 
 
 
 
  1. # define model  
  2. model = XGBRegressor() 

現(xiàn)在我們已經(jīng)熟悉了XGBoost,下面讓我們看一下如何為監(jiān)督學(xué)習(xí)準(zhǔn)備時間序列數(shù)據(jù)集。

時間序列數(shù)據(jù)準(zhǔn)備

時間序列數(shù)據(jù)可以表述為監(jiān)督學(xué)習(xí)。給定時間序列數(shù)據(jù)集的數(shù)字序列,我們可以將數(shù)據(jù)重組為看起來像監(jiān)督學(xué)習(xí)的問題。我們可以通過使用以前的時間步長作為輸入變量,并使用下一個時間步長作為輸出變量來做到這一點。讓我們通過一個例子來具體說明。假設(shè)我們有一個時間序列,如下所示:

 
 
 
 
  1. time, measure  
  2. 1, 100  
  3. 2, 110  
  4. 3, 108  
  5. 4, 115  
  6. 5, 120 

通過使用上一個時間步的值來預(yù)測下一個時間步的值,我們可以將此時間序列數(shù)據(jù)集重組為監(jiān)督學(xué)習(xí)問題。通過這種方式重組時間序列數(shù)據(jù)集,數(shù)據(jù)將如下所示:

 
 
 
 
  1. X, y  
  2. ?, 100  
  3. 100, 110  
  4. 110, 108  
  5. 108, 115  
  6. 115, 120  
  7. 120, ? 

請注意,時間列已刪除,某些數(shù)據(jù)行不可用于訓(xùn)練模型,例如第一和最后一個。

這種表示稱為滑動窗口,因為輸入和預(yù)期輸出的窗口會隨著時間向前移動,從而為監(jiān)督學(xué)習(xí)模型創(chuàng)建新的“樣本”。

有關(guān)準(zhǔn)備時間序列預(yù)測數(shù)據(jù)的滑動窗口方法的更多信息。

在給定所需的輸入和輸出序列長度的情況下,我們可以在Pandas中使用shift()函數(shù)自動創(chuàng)建時間序列問題的新框架。

這將是一個有用的工具,因為它將允許我們使用機器學(xué)習(xí)算法探索時間序列問題的不同框架,以查看可能導(dǎo)致性能更好的模型。

下面的函數(shù)將一個時間序列作為具有一個或多個列的NumPy數(shù)組時間序列,并將其轉(zhuǎn)換為具有指定數(shù)量的輸入和輸出的監(jiān)督學(xué)習(xí)問題。

 
 
 
 
  1. # transform a time series dataset into a supervised learning dataset  
  2. def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):  
  3.  n_vars = 1 if type(data) is list else data.shape[1]  
  4.  df = DataFrame(data)  
  5.  cols = list()  
  6.  # input sequence (t-n, ... t-1)  
  7.  for i in range(n_in, 0, -1):  
  8.   cols.append(df.shift(i))  
  9.  # forecast sequence (t, t+1, ... t+n)  
  10.  for i in range(0, n_out):  
  11.   cols.append(df.shift(-i))  
  12.  # put it all together  
  13.  agg = concat(cols, axis=1)  
  14.  # drop rows with NaN values  
  15.  if dropnan:  
  16.   agg.dropna(inplace=True)  
  17.  return agg.values 

我們可以使用此函數(shù)為XGBoost準(zhǔn)備時間序列數(shù)據(jù)集。

準(zhǔn)備好數(shù)據(jù)集后,我們必須小心如何使用它來擬合和評估模型。

例如,將模型擬合未來的數(shù)據(jù)并預(yù)測過去是無效的。該模型必須在過去進行訓(xùn)練并預(yù)測未來。這意味著不能使用在評估過程中將數(shù)據(jù)集隨機化的方法,例如k折交叉驗證。相反,我們必須使用一種稱為前向驗證的技術(shù)。在前向驗證中,首先通過選擇一個切點(例如除過去12個月外,所有數(shù)據(jù)均用于培訓(xùn),最近12個月用于測試。

如果我們有興趣進行單步預(yù)測,例如一個月后,我們可以通過對訓(xùn)練數(shù)據(jù)集進行訓(xùn)練并預(yù)測測試數(shù)據(jù)集的第一步來評估模型。然后,我們可以將來自測試集的真實觀測值添加到訓(xùn)練數(shù)據(jù)集中,重新擬合模型,然后讓模型預(yù)測測試數(shù)據(jù)集中的第二步。對整個測試數(shù)據(jù)集重復(fù)此過程將為整個測試數(shù)據(jù)集提供一步式預(yù)測,可以從中計算出誤差度量以評估模型的技能。

下面的函數(shù)執(zhí)行前向驗證。它使用時間序列數(shù)據(jù)集的整個監(jiān)督學(xué)習(xí)版本以及用作測試集的行數(shù)作為參數(shù)。然后,它逐步通過測試集,調(diào)用xgboost_forecast()函數(shù)進行單步預(yù)測。計算錯誤度量,并將詳細信息返回以進行分析。

 
 
 
 
  1. # walk-forward validation for univariate data  
  2. def walk_forward_validation(data, n_test):  
  3.  predictions = list()  
  4.  # split dataset  
  5.  train, test = train_test_split(data, n_test)  
  6.  # seed history with training dataset  
  7.  history = [x for x in train]  
  8.  # step over each time-step in the test set  
  9.  for i in range(len(test)):  
  10.   # split test row into input and output columns  
  11.   testX, testtesty = test[i, :-1], test[i, -1]  
  12.   # fit model on history and make a prediction  
  13.   yhat = xgboost_forecast(history, testX)  
  14.   # store forecast in list of predictions  
  15.   predictions.append(yhat)  
  16.   # add actual observation to history for the next loop  
  17.   history.append(test[i])  
  18.   # summarize progress  
  19.   print('>expected=%.1f, predicted=%.1f' % (testy, yhat))  
  20.  # estimate prediction error  
  21.  error = mean_absolute_error(test[:, -1], predictions)  
  22.  return error, test[:, 1], predictions 

調(diào)用train_test_split()函數(shù)可將數(shù)據(jù)集拆分為訓(xùn)練集和測試集。我們可以在下面定義此功能。

 
 
 
 
  1. # split a univariate dataset into train/test sets  
  2. def train_test_split(data, n_test): 
  3.  return data[:-n_test, :], data[-n_test:, :] 

我們可以使用XGBRegressor類進行單步預(yù)測。下面的xgboost_forecast()函數(shù)通過將訓(xùn)練數(shù)據(jù)集和測試輸入行作為輸入,擬合模型并進行單步預(yù)測來實現(xiàn)此目的。

 
 
 
 
  1. # fit an xgboost model and make a one step prediction  
  2. def xgboost_forecast(train, testX):  
  3.  # transform list into array  
  4.  train = asarray(train)  
  5.  # split into input and output columns  
  6.  trainX, traintrainy = train[:, :-1], train[:, -1]  
  7.  # fit model  
  8.  model = XGBRegressor(objective='reg:squarederror', n_estimators=1000)  
  9.  model.fit(trainX, trainy)  
  10.  # make a one-step prediction  
  11.  yhat = model.predict([testX])  
  12.  return yhat[0] 

現(xiàn)在,我們知道了如何準(zhǔn)備時間序列數(shù)據(jù)以進行預(yù)測和評估XGBoost模型,接下來我們可以看看在實際數(shù)據(jù)集上使用XGBoost的情況。

XGBoost用于時間序列預(yù)測

在本節(jié)中,我們將探索如何使用XGBoost進行時間序列預(yù)測。我們將使用標(biāo)準(zhǔn)的單變量時間序列數(shù)據(jù)集,以使用該模型進行單步預(yù)測。您可以將本節(jié)中的代碼用作您自己項目的起點,并輕松地對其進行調(diào)整以適應(yīng)多變量輸入,多變量預(yù)測和多步預(yù)測。我們將使用每日女性出生數(shù)據(jù)集,即三年中的每月出生數(shù)。

您可以從此處下載數(shù)據(jù)集,并將其放在文件名“ daily-total-female-births.csv”的當(dāng)前工作目錄中。

數(shù)據(jù)集(每天女性出生總數(shù).csv):

 
 
 
 
  1. https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.csv 

說明(每日女性出生總數(shù)):

 
 
 
 
  1. https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.names 

數(shù)據(jù)集的前幾行如下所示:

 
 
 
 
  1. "Date","Births"  
  2. "1959-01-01",35  
  3. "1959-01-02",32  
  4. "1959-01-03",30  
  5. "1959-01-04",31  
  6. "1959-01-05",44  
  7. ... 

首先,讓我們加載并繪制數(shù)據(jù)集。下面列出了完整的示例。

 
 
 
 
  1. # load and plot the time series dataset  
  2. from pandas import read_csv  
  3. from matplotlib import pyplot  
  4. # load dataset  
  5. series = read_csv('daily-total-female-births.csv', header=0, index_col=0)  
  6. values = series.values  
  7. # plot dataset  
  8. pyplot.plot(values) 
  9. pyplot.show() 

運行示例將創(chuàng)建數(shù)據(jù)集的折線圖。我們可以看到?jīng)]有明顯的趨勢或季節(jié)性。

當(dāng)預(yù)測最近的12個月時,持久性模型可以實現(xiàn)約6.7例出生的MAE。這提供了性能基準(zhǔn),在該基準(zhǔn)之上可以認為模型是熟練的。

接下來,當(dāng)對過去12個月的數(shù)據(jù)進行單步預(yù)測時,我們可以評估數(shù)據(jù)集上的XGBoost模型。

我們將僅使用前6個時間步長作為模型和默認模型超參數(shù)的輸入,除了我們將損失更改為'reg:squarederror'(以避免警告消息),并在集合中使用1,000棵樹(以避免學(xué)習(xí)不足) )。

下面列出了完整的示例。

 
 
 
 
  1. # forecast monthly births with xgboost  
  2. from numpy import asarray  
  3. from pandas import read_csv 
  4. from pandas import DataFrame 
  5. from pandas import concat  
  6. from sklearn.metrics import mean_absolute_error  
  7. from xgboost import XGBRegressor  
  8. from matplotlib import pyplot   
  9. # transform a time series dataset into a supervised learning dataset  
  10. def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):  
  11.  n_vars = 1 if type(data) is list else data.shape[1]  
  12.  df = DataFrame(data)  
  13.  cols = list()  
  14.  # input sequence (t-n, ... t-1)  
  15.  for i in range(n_in, 0, -1):  
  16.   cols.append(df.shift(i))  
  17.  # forecast sequence (t, t+1, ... t+n)  
  18.  for i in range(0, n_out):  
  19.   cols.append(df.shift(-i))  
  20.  # put it all together  
  21.  agg = concat(cols, axis=1)  
  22.  # drop rows with NaN values  
  23.  if dropnan:  
  24.   agg.dropna(inplace=True)  
  25.  return agg.values   
  26. # split a univariate dataset into train/test sets  
  27. def train_test_split(data, n_test):  
  28.  return data[:-n_test, :], data[-n_test:, :] 
  29. # fit an xgboost model and make a one step prediction  
  30. def xgboost_forecast(train, testX):  
  31.  # transform list into array  
  32.  train = asarray(train)  
  33.  # split into input and output columns  
  34.  trainX, traintrainy = train[:, :-1], train[:, -1]  
  35.  # fit model  
  36.  model = XGBRegressor(objective='reg:squarederror', n_estimators=1000)  
  37.  model.fit(trainX, trainy)  
  38.  # make a one-step prediction  
  39.  yhat = model.predict(asarray([testX]))  
  40.  return yhat[0]  
  41. # walk-forward validation for univariate data  
  42. def walk_forward_validation(data, n_test):  
  43.  predictions = list()  
  44.  # split dataset  
  45.  train, test = train_test_split(data, n_test)  
  46.  # seed history with training dataset  
  47.  history = [x for x in train]  
  48.  # step over each time-step in the test set  
  49.  for i in range(len(test)):  
  50.   # split test row into input and output columns  
  51.   testX, testtesty = test[i, :-1], test[i, -1]  
  52.   # fit model on history and make a prediction  
  53.   yhat = xgboost_forecast(history, testX)  
  54.   # store forecast in list of predictions  
  55.   predictions.append(yhat)  
  56.   # add actual observation to history for the next loop  
  57.   history.append(test[i])  
  58.   # summarize progress  
  59.   print('>expected=%.1f, predicted=%.1f' % (testy, yhat))  
  60.  # estimate prediction error  
  61.  error = mean_absolute_error(test[:, -1], predictions)  
  62.  return error, test[:, -1], predictions  
  63. # load the dataset  
  64. series = read_csv('daily-total-female-births.csv', header=0, index_col=0)  
  65. values = series.values  
  66. # transform the time series data into supervised learning  
  67. data = series_to_supervised(values, n_in=6)  
  68. # evaluate  
  69. mae, y, yhat = walk_forward_validation(data, 12)  
  70. print('MAE: %.3f' % mae)  
  71. # plot expected vs preducted  
  72. pyplot.plot(y, label='Expected')  
  73. pyplot.plot(yhat, label='Predicted')  
  74. pyplot.legend()  
  75. pyplot.show() 

運行示例將報告測試集中每個步驟的期望值和預(yù)測值,然后報告所有預(yù)測值的MAE。

注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同??紤]運行該示例幾次并比較平均結(jié)果。

我們可以看到,該模型的性能優(yōu)于持久性模型,MAE約為5.9,而MAE約為6.7

 
 
 
 
  1. >expected=42.0, predicted=44.5  
  2. >expected=53.0, predicted=42.5  
  3. >expected=39.0, predicted=40.3  
  4. >expected=40.0, predicted=32.5  
  5. >expected=38.0, predicted=41.1  
  6. >expected=44.0, predicted=45.3  
  7. >expected=34.0, predicted=40.2  
  8. >expected=37.0, predicted=35.0  
  9. >expected=52.0, predicted=32.5  
  10. >expected=48.0, predicted=41.4  
  11. >expected=55.0, predicted=46.6  
  12. >expected=50.0, predicted=47.2  
  13. MAE: 5.957 

創(chuàng)建線圖,比較數(shù)據(jù)集最后12個月的一系列期望值和預(yù)測值。這給出了模型在測試集上執(zhí)行得如何的幾何解釋。

圖2

一旦選擇了最終的XGBoost模型配置,就可以最終確定模型并用于對新數(shù)據(jù)進行預(yù)測。這稱為樣本外預(yù)測,例如 超出訓(xùn)練數(shù)據(jù)集進行預(yù)測。這與在模型評估期間進行預(yù)測是相同的:因為我們始終希望使用模型用于對新數(shù)據(jù)進行預(yù)測時所期望使用的相同過程來評估模型。下面的示例演示了在所有可用數(shù)據(jù)上擬合最終XGBoost模型并在數(shù)據(jù)集末尾進行單步預(yù)測的過程。

 
 
 
 
  1. # finalize model and make a prediction for monthly births with xgboost  
  2. from numpy import asarray  
  3. from pandas import read_csv  
  4. from pandas import DataFrame  
  5. from pandas import concat  
  6. from xgboost import XGBRegressor   
  7. # transform a time series dataset into a supervised learning dataset  
  8. def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):  
  9.  n_vars = 1 if type(data) is list else data.shape[1]  
  10.  df = DataFrame(data)  
  11.  cols = list()  
  12.  # input sequence (t-n, ... t-1)  
  13.  for i in range(n_in, 0, -1):  
  14.   cols.append(df.shift(i))  
  15.  # forecast sequence (t, t+1, ... t+n)  
  16.  for i in range(0, n_out):  
  17.   cols.append(df.shift(-i))  
  18.  # put it all together  
  19.  agg = concat(cols, axis=1)  
  20.  # drop rows with NaN values  
  21.  if dropnan:  
  22.   agg.dropna(inplace=True)  
  23.  return agg.values   
  24. # load the dataset  
  25. series = read_csv('daily-total-female-births.csv', header=0, index_col=0)  
  26. values = series.values  
  27. # transform the time series data into supervised learning  
  28. train = series_to_supervised(values, n_in=6)  
  29. # split into input and output columns  
  30. trainX, traintrainy = train[:, :-1], train[:, -1] 
  31. # fit model  
  32. model = XGBRegressor(objective='reg:squarederror', n_estimators=1000)  
  33. model.fit(trainX, trainy)  
  34. # construct an input for a new preduction  
  35. row = values[-6:].flatten()  
  36. # make a one-step prediction  
  37. yhat = model.predict(asarray([row]))  
  38. print('Input: %s, Predicted: %.3f' % (row, yhat[0])) 

運行示例將XGBoost模型適合所有可用數(shù)據(jù)。使用最近6個月的已知數(shù)據(jù)準(zhǔn)備新的輸入行,并預(yù)測數(shù)據(jù)集結(jié)束后的下個月。

 
 
 
 
  1. Input: [34 37 52 48 55 50], Predicted: 42.708  

當(dāng)前文章:用XGBoost進行時間序列預(yù)測
鏈接分享:http://m.5511xx.com/article/dpcedpj.html