1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 14504字,需瀏覽 30分鐘

         ·

        2021-04-07 17:11

        XGBoost是梯度分類和回歸問題的有效實現(xiàn)。
        它既快速又高效,即使在各種預(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進行安裝,如下所示:
        sudo pip install xgboost

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

        # xgboost
        import xgboost
        print("xgboost", xgboost.__version__)
        運行代碼,您應(yīng)該看到以下版本號或更高版本。
        xgboost 1.0.1
        盡管XGBoost庫具有自己的Python API,但我們可以通過XGBRegressor包裝器類將XGBoost模型與scikit-learn API結(jié)合使用。
        可以實例化模型的實例,就像將其用于模型評估的任何其他scikit-learn類一樣使用。例如:
        # define model
        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è)我們有一個時間序列,如下所示:
        time, measure
        1100
        2110
        3108
        4115
        5120
        通過使用上一個時間步的值來預(yù)測下一個時間步的值,我們可以將此時間序列數(shù)據(jù)集重組為監(jiān)督學(xué)習(xí)問題。通過這種方式重組時間序列數(shù)據(jù)集,數(shù)據(jù)將如下所示:
        X, y
        ?, 100
        100110
        110108
        108115
        115120
        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í)問題。
        # transform a time series dataset into a supervised learning dataset
        def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
         n_vars = 1 if type(data) is list else data.shape[1]
         df = DataFrame(data)
         cols = list()
         # input sequence (t-n, ... t-1)
         for i in range(n_in, 0-1):
          cols.append(df.shift(i))
         # forecast sequence (t, t+1, ... t+n)
         for i in range(0, n_out):
          cols.append(df.shift(-i))
         # put it all together
         agg = concat(cols, axis=1)
         # drop rows with NaN values
         if dropnan:
          agg.dropna(inplace=True)
         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ù)測。計算錯誤度量,并將詳細(xì)信息返回以進行分析。
        # walk-forward validation for univariate data
        def walk_forward_validation(data, n_test):
         predictions = list()
         # split dataset
         train, test = train_test_split(data, n_test)
         # seed history with training dataset
         history = [x for x in train]
         # step over each time-step in the test set
         for i in range(len(test)):
          # split test row into input and output columns
          testX, testy = test[i, :-1], test[i, -1]
          # fit model on history and make a prediction
          yhat = xgboost_forecast(history, testX)
          # store forecast in list of predictions
          predictions.append(yhat)
          # add actual observation to history for the next loop
          history.append(test[i])
          # summarize progress
          print('>expected=%.1f, predicted=%.1f' % (testy, yhat))
         # estimate prediction error
         error = mean_absolute_error(test[:, -1], predictions)
         return error, test[:, 1], predictions
        調(diào)用train_test_split()函數(shù)可將數(shù)據(jù)集拆分為訓(xùn)練集和測試集。我們可以在下面定義此功能。
        # split a univariate dataset into train/test sets
        def train_test_split(data, n_test):
         return data[:-n_test, :], data[-n_test:, :]
        我們可以使用XGBRegressor類進行單步預(yù)測。下面的xgboost_forecast()函數(shù)通過將訓(xùn)練數(shù)據(jù)集和測試輸入行作為輸入,擬合模型并進行單步預(yù)測來實現(xiàn)此目的。
        # fit an xgboost model and make a one step prediction
        def xgboost_forecast(train, testX):
         # transform list into array
         train = asarray(train)
         # split into input and output columns
         trainX, trainy = train[:, :-1], train[:, -1]
         # fit model
         model = XGBRegressor(objective='reg:squarederror', n_estimators=1000)
         model.fit(trainX, trainy)
         # make a one-step prediction
         yhat = model.predict([testX])
         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):
        https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.csv
        說明(每日女性出生總數(shù)):
        https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-total-female-births.names
        數(shù)據(jù)集的前幾行如下所示:
        "Date","Births"
        "1959-01-01",35
        "1959-01-02",32
        "1959-01-03",30
        "1959-01-04",31
        "1959-01-05",44
        ...
        首先,讓我們加載并繪制數(shù)據(jù)集。下面列出了完整的示例。
        # load and plot the time series dataset
        from pandas import read_csv
        from matplotlib import pyplot
        # load dataset
        series = read_csv('daily-total-female-births.csv', header=0, index_col=0)
        values = series.values
        # plot dataset
        pyplot.plot(values)
        pyplot.show()
        運行示例將創(chuàng)建數(shù)據(jù)集的折線圖。我們可以看到?jīng)]有明顯的趨勢或季節(jié)性。


        當(dāng)預(yù)測最近的12個月時,持久性模型可以實現(xiàn)約6.7例出生的MAE。這提供了性能基準(zhǔn),在該基準(zhǔn)之上可以認(rèn)為模型是熟練的。
        接下來,當(dāng)對過去12個月的數(shù)據(jù)進行單步預(yù)測時,我們可以評估數(shù)據(jù)集上的XGBoost模型。
        我們將僅使用前6個時間步長作為模型和默認(rèn)模型超參數(shù)的輸入,除了我們將損失更改為'reg:squarederror'(以避免警告消息),并在集合中使用1,000棵樹(以避免學(xué)習(xí)不足) )。
        下面列出了完整的示例。
        # forecast monthly births with xgboost
        from numpy import asarray
        from pandas import read_csv
        from pandas import DataFrame
        from pandas import concat
        from sklearn.metrics import mean_absolute_error
        from xgboost import XGBRegressor
        from matplotlib import pyplot
         
        # transform a time series dataset into a supervised learning dataset
        def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
         n_vars = 1 if type(data) is list else data.shape[1]
         df = DataFrame(data)
         cols = list()
         # input sequence (t-n, ... t-1)
         for i in range(n_in, 0-1):
          cols.append(df.shift(i))
         # forecast sequence (t, t+1, ... t+n)
         for i in range(0, n_out):
          cols.append(df.shift(-i))
         # put it all together
         agg = concat(cols, axis=1)
         # drop rows with NaN values
         if dropnan:
          agg.dropna(inplace=True)
         return agg.values
         
        # split a univariate dataset into train/test sets
        def train_test_split(data, n_test):
         return data[:-n_test, :], data[-n_test:, :]
         
        # fit an xgboost model and make a one step prediction
        def xgboost_forecast(train, testX):
         # transform list into array
         train = asarray(train)
         # split into input and output columns
         trainX, trainy = train[:, :-1], train[:, -1]
         # fit model
         model = XGBRegressor(objective='reg:squarederror', n_estimators=1000)
         model.fit(trainX, trainy)
         # make a one-step prediction
         yhat = model.predict(asarray([testX]))
         return yhat[0]
         
        # walk-forward validation for univariate data
        def walk_forward_validation(data, n_test):
         predictions = list()
         # split dataset
         train, test = train_test_split(data, n_test)
         # seed history with training dataset
         history = [x for x in train]
         # step over each time-step in the test set
         for i in range(len(test)):
          # split test row into input and output columns
          testX, testy = test[i, :-1], test[i, -1]
          # fit model on history and make a prediction
          yhat = xgboost_forecast(history, testX)
          # store forecast in list of predictions
          predictions.append(yhat)
          # add actual observation to history for the next loop
          history.append(test[i])
          # summarize progress
          print('>expected=%.1f, predicted=%.1f' % (testy, yhat))
         # estimate prediction error
         error = mean_absolute_error(test[:, -1], predictions)
         return error, test[:, -1], predictions
         
        # load the dataset
        series = read_csv('daily-total-female-births.csv', header=0, index_col=0)
        values = series.values
        # transform the time series data into supervised learning
        data = series_to_supervised(values, n_in=6)
        # evaluate
        mae, y, yhat = walk_forward_validation(data, 12)
        print('MAE: %.3f' % mae)
        # plot expected vs preducted
        pyplot.plot(y, label='Expected')
        pyplot.plot(yhat, label='Predicted')
        pyplot.legend()
        pyplot.show()
        運行示例將報告測試集中每個步驟的期望值和預(yù)測值,然后報告所有預(yù)測值的MAE。
        注意:由于算法或評估程序的隨機性,或者數(shù)值精度的差異,您的結(jié)果可能會有所不同??紤]運行該示例幾次并比較平均結(jié)果。
        我們可以看到,該模型的性能優(yōu)于持久性模型,MAE約為5.9,而MAE約為6.7
        >expected=42.0, predicted=44.5
        >expected=53.0, predicted=42.5
        >expected=39.0, predicted=40.3
        >expected=40.0, predicted=32.5
        >expected=38.0, predicted=41.1
        >expected=44.0, predicted=45.3
        >expected=34.0, predicted=40.2
        >expected=37.0, predicted=35.0
        >expected=52.0, predicted=32.5
        >expected=48.0, predicted=41.4
        >expected=55.0, predicted=46.6
        >expected=50.0, predicted=47.2
        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ù)測的過程。
        # finalize model and make a prediction for monthly births with xgboost
        from numpy import asarray
        from pandas import read_csv
        from pandas import DataFrame
        from pandas import concat
        from xgboost import XGBRegressor
         
        # transform a time series dataset into a supervised learning dataset
        def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
         n_vars = 1 if type(data) is list else data.shape[1]
         df = DataFrame(data)
         cols = list()
         # input sequence (t-n, ... t-1)
         for i in range(n_in, 0-1):
          cols.append(df.shift(i))
         # forecast sequence (t, t+1, ... t+n)
         for i in range(0, n_out):
          cols.append(df.shift(-i))
         # put it all together
         agg = concat(cols, axis=1)
         # drop rows with NaN values
         if dropnan:
          agg.dropna(inplace=True)
         return agg.values
         
        # load the dataset
        series = read_csv('daily-total-female-births.csv', header=0, index_col=0)
        values = series.values
        # transform the time series data into supervised learning
        train = series_to_supervised(values, n_in=6)
        # split into input and output columns
        trainX, trainy = train[:, :-1], train[:, -1]
        # fit model
        model = XGBRegressor(objective='reg:squarederror', n_estimators=1000)
        model.fit(trainX, trainy)
        # construct an input for a new preduction
        row = values[-6:].flatten()
        # make a one-step prediction
        yhat = model.predict(asarray([row]))
        print('Input: %s, Predicted: %.3f' % (row, yhat[0]))
        運行示例將XGBoost模型適合所有可用數(shù)據(jù)。使用最近6個月的已知數(shù)據(jù)準(zhǔn)備新的輸入行,并預(yù)測數(shù)據(jù)集結(jié)束后的下個月。
        Input: [34 37 52 48 55 50], Predicted: 42.708


        者:沂水寒城,CSDN博客專家,個人研究方向:機器學(xué)習(xí)、深度學(xué)習(xí)、NLP、CV

        Blog: http://yishuihancheng.blog.csdn.net


        贊 賞 作 者



        更多閱讀



        用 Python 從零開始實現(xiàn)簡單遺傳算法


        5分鐘掌握 Python 隨機爬山算法


        5分鐘完全讀懂關(guān)聯(lián)規(guī)則挖掘算法

        特別推薦




        點擊下方閱讀原文加入社區(qū)會員

        瀏覽 118
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            欧洲三级网观看 | 日本不卡高清视频 | 草逼动态图 | 亚洲免费在线视频 | 波多野结衣的毛片 | 啪啪视频免费观看 | 国产精品久久久春嫩 | 在电影院被他摸的好爽 | 少妇口述性高潮 | 攻受男男肉不打码视频免费 |