<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 如何建立預測大氣污染日的概率預測模型 > 原文: [https://machinelearningmastery.com/how-to-develop-a-probabilistic-forecasting-model-to-predict-air-pollution-days/](https://machinelearningmastery.com/how-to-develop-a-probabilistic-forecasting-model-to-predict-air-pollution-days/) 空氣污染的特點是地面臭氧濃度。 根據風速和溫度等氣象測量結果,可以預測明天地面臭氧是否會達到足夠高的水平,以發布公共空氣污染預警。 這是用于時間序列分類數據集的標準機器學習數據集的基礎,簡稱為“_ 臭氧預測問題 _”。該數據集描述了休斯頓地區七年來的氣象觀測以及臭氧水平是否高于臨界空氣污染水平。 在本教程中,您將了解如何探索這些數據并開發概率預測模型,以預測德克薩斯州休斯頓的空氣污染。 完成本教程后,您將了解: * 如何加載和準備臭氧日標準機器學習預測建模問題。 * 如何開發一個樸素的預測模型,并使用 Brier 技能分數評估預測。 * 如何使用決策樹集合開發技巧模型,并通過成功模型的超參數調整進一步提高技能。 讓我們開始吧。 ![How to Develop a Probabilistic Forecasting Model to Predict Air Pollution Days](https://img.kancloud.cn/92/5b/925b266f8221e47749f03d9798be3684_640x427.jpg) 如何建立預測空氣污染日的概率預測模型 照片由 [paramita](https://www.flickr.com/photos/parramitta/6715213551/) ,保留一些權利。 ## 教程概述 本教程分為五個部分;他們是: 1. 臭氧預測問題 2. 加載和檢查數據 3. 樸素預測模型 4. 集合樹預測模型 5. 調整梯度提升 ## 臭氧預測問題 空氣污染的特征在于地面臭氧的高度測量,通常被稱為“[不良臭氧](https://en.wikipedia.org/wiki/Tropospheric_ozone)”,以區??別于高層大氣中的臭氧。 臭氧預測問題是時間序列分類預測問題,其涉及預測第二天是否將是高空氣污染日(臭氧日)。氣象組織可以利用臭氧日的預測來警告公眾,使他們能夠采取預防措施。 該數據集最初由 Kun Zhang 等人研究。他們在 2006 年的論文“[預測偏差隨機臭氧天數:分析和解決方案](https://ieeexplore.ieee.org/abstract/document/4053100/)”,然后在他們的后續報告中再次提到“[預測偏差隨機臭氧天:分析,解決方案及其他](https://link.springer.com/article/10.1007/s10115-007-0095-1) “。 這是一個具有挑戰性的問題,因為高臭氧水平的物理機制是(或沒有)被完全理解,這意味著預測不能像其他氣象預報一樣基于物理模擬,如溫度和降雨量。 該數據集被用作開發預測模型的基礎,該模型使用一系列可能與預測臭氧水平相關或可能不相關的變量,此外還有少數已知與所涉及的實際化學過程相關的變量。 > 然而,環境科學家普遍認為,目前從未探索過的大量其他特征在建立高度精確的臭氧預測模型中非常有用。然而,鮮為人知的是這些特征究竟是什么以及它們如何在臭氧形成中實際相互作用。 [...]今天的環境科學都沒有知道如何使用它們。這為數據挖掘提供了絕佳的機會 - [預測偏差隨機臭氧天數:分析和解決方案](https://ieeexplore.ieee.org/abstract/document/4053100/),2006 年。 在隨后的一天預測高水平的地面臭氧是一個具有挑戰性的問題,已知其具有隨機性。這意味著預計會出現預測錯誤。因此,希望概率地對預測問題進行建模并預測臭氧日的概率,或者在前一天或幾天給出觀察結果。 該數據集包含七年的每日氣象變量觀測值(1998-2004 或 2,536 天)以及是否有臭氧日,在美國德克薩斯州的休斯頓,加爾維斯頓和布拉佐里亞地區進行。 每天共觀察到 72 個變量,其中許多被認為與預測問題相關,其中 10 個已根據物理學確認為相關。 > [...]這 72 個特征中只有大約 10 個特征已經被環境科學家證實是有用和相關的,并且關于其他 60 個特征的相關性,既沒有經驗也沒有理論信息。然而,空氣質量控制科學家長期以來一直在猜測這些特征中的一些可能是有用的,但是無法發展理論或使用模擬來證明其相關性。 - [預測偏差隨機臭氧天數:分析和解決方案](https://ieeexplore.ieee.org/abstract/document/4053100/),2006 年。 有 24 個變量跟蹤每小時風速,另外 24 個變量跟蹤一天中每小時的溫度。數據集的兩個版本可用于度量的不同平均周期,特別是 1 小時和 8 小時。 似乎缺少并可能有用的是每天觀察到的臭氧,而不是二氧化硫臭氧日/非臭氧日。參數模型中使用的其他度量也不可用。 有趣的是,基于“[開發臭氧預測計劃指南](https://nepis.epa.gov/Exe/ZyPURL.cgi?Dockey=2000D6KP.TXT)”,1999 年 EPA 指南中的描述,使用參數臭氧預測模型作為基線。該文件還描述了驗證臭氧預報系統的標準方法。 總之,這是一個具有挑戰性的預測問題,因為: * 存在大量變量,其重要性通常是未知的。 * 輸入變量及其相互關系可能會隨時間而變化。 * 對于需要處理的許多變量缺少觀察結果。 * 非臭氧日(非事件)遠遠超過臭氧日(事件),使得這些類別高度不平衡。 ## 加載和檢查數據 該數據集可從 UCI 機器學習庫獲得。 * [臭氧水平檢測數據集](https://archive.ics.uci.edu/ml/datasets/ozone+level+detection) 我們只會查看本教程中的 8 小時數據。下載“ _eighthr.data_ ”并將其放在當前的工作目錄中。 檢查數據文件,我們可以看到不同尺度的觀察結果。 ```py 1/1/1998,0.8,1.8,2.4,2.1,2,2.1,1.5,1.7,1.9,2.3,3.7,5.5,5.1,5.4,5.4,4.7,4.3,3.5,3.5,2.9,3.2,3.2,2.8,2.6,5.5,3.1,5.2,6.1,6.1,6.1,6.1,5.6,5.2,5.4,7.2,10.6,14.5,17.2,18.3,18.9,19.1,18.9,18.3,17.3,16.8,16.1,15.4,14.9,14.8,15,19.1,12.5,6.7,0.11,3.83,0.14,1612,-2.3,0.3,7.18,0.12,3178.5,-15.5,0.15,10.67,-1.56,5795,-12.1,17.9,10330,-55,0,0. 1/2/1998,2.8,3.2,3.3,2.7,3.3,3.2,2.9,2.8,3.1,3.4,4.2,4.5,4.5,4.3,5.5,5.1,3.8,3,2.6,3,2.2,2.3,2.5,2.8,5.5,3.4,15.1,15.3,15.6,15.6,15.9,16.2,16.2,16.2,16.6,17.8,19.4,20.6,21.2,21.8,22.4,22.1,20.8,19.1,18.1,17.2,16.5,16.1,16,16.2,22.4,17.8,9,0.25,-0.41,9.53,1594.5,-2.2,0.96,8.24,7.3,3172,-14.5,0.48,8.39,3.84,5805,14.05,29,10275,-55,0,0. 1/3/1998,2.9,2.8,2.6,2.1,2.2,2.5,2.5,2.7,2.2,2.5,3.1,4,4.4,4.6,5.6,5.4,5.2,4.4,3.5,2.7,2.9,3.9,4.1,4.6,5.6,3.5,16.6,16.7,16.7,16.8,16.8,16.8,16.9,16.9,17.1,17.6,19.1,21.3,21.8,22,22.1,22.2,21.3,19.8,18.6,18,18,18.2,18.3,18.4,22.2,18.7,9,0.56,0.89,10.17,1568.5,0.9,0.54,3.8,4.42,3160,-15.9,0.6,6.94,9.8,5790,17.9,41.3,10235,-40,0,0. ... ``` 瀏覽文件,例如到 2003 年初,我們可以看到缺少的觀察值標有“?”值。 ```py ... 12/29/2002,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,11.7,0.09,5.59,3.79,1578,5.7,0.04,1.8,4.8,3181.5,-13,0.02,0.38,2.78,5835,-31.1,18.9,10250,-25,0.03,0. 12/30/2002,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,10.3,0.43,3.88,9.21,1525.5,1.8,0.87,9.17,9.96,3123,-11.3,0.03,11.23,10.79,5780,17,30.2,10175,-75,1.68,0. 12/31/2002,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,8.5,0.96,6.05,11.18,1433,-0.85,0.91,7.02,6.63,3014,-16.2,0.05,15.77,24.38,5625,31.15,48.75,10075,-100,0.05,0. 1/1/2003,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,7.2,5.7,4.5,4,3.6,3.3,3.1,3.2,6.7,11.1,13.8,15.8,17.2,18.6,20,21.1,21.5,20.4,19.1,17.8,17.4,16.9,16.6,14.9,21.5,12.6,6.4,0.6,12.91,-10.17,1421.5,1.95,0.55,11.97,-7.78,3006.5,-14.1,0.44,20.42,-13.31,5640,2.9,30.5,10095,35,0,0. ... ``` 首先,我們可以使用 [read_csv()函數](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html)將數據作為 Pandas DataFrame 加載。沒有數據頭,我們可以解析第一列中的日期并將它們用作索引;下面列出了完整的示例。 ```py # load and summarize from pandas import read_csv from matplotlib import pyplot # load dataset data = read_csv('eighthr.data', header=None, index_col=0, parse_dates=True, squeeze=True) print(data.shape) # summarize class counts counts = data.groupby(73).size() for i in range(len(counts)): percent = counts[i] / data.shape[0] * 100 print('Class=%d, total=%d, percentage=%.3f' % (i, counts[i], percent)) ``` 運行該示例確認有 2,534 天的數據和 73 個變量。 我們還可以看到階級不平衡的性質,其中 93%以上的日子是非臭氧日,約 6%是臭氧日。 ```py (2534, 73) Class=0, total=2374, percentage=93.686 Class=1, total=160, percentage=6.314 ``` 我們還可以在七年內創建輸出變量的線圖,以了解臭氧天數是否發生在一年中的任何特定時間。 ```py # load and plot output variable from pandas import read_csv from matplotlib import pyplot # load dataset data = read_csv('eighthr.data', header=None, index_col=0, parse_dates=True, squeeze=True) # plot the output variable pyplot.plot(data.index, data.values[:,-1]) pyplot.show() ``` 運行該示例將創建七年內輸出變量的線圖。 我們可以看到,每年中期都有臭氧天集群:北半球夏季或溫暖的月份。 ![Line plot of output variable over 7 years](https://img.kancloud.cn/38/b1/38b1a1999952dd02e7ab5595025d3325_1280x960.jpg) 輸出變量的線圖超過 7 年 通過簡要回顧一下觀察結果,我們可以得到一些關于如何準備數據的想法: * 缺少數據需要處理。 * 最簡單的框架是根據今天的觀察結果預測明天的臭氧日。 * 溫度可能與季節或一年中的時間相關,可能是一個有用的預測指標。 * 數據變量可能需要縮放(標準化),甚至可能需要標準化,具體取決于所選的算法。 * 預測概率將提供比預測類值更多的細微差別。 * 也許我們可以使用五年(約 72%)來訓練模型并在剩余的兩年內測試它(約 28%) 我們可以執行一些最小的數據準備。 下面的示例加載數據集,用 0.0 替換缺失的觀測值,將數據構建為監督學習問題(明天根據今天的觀察結果預測臭氧),并根據大量天數將數據分成訓練和測試集。兩年。 您可以探索替換缺失值的替代方法,例如輸入平均值。此外,2004 年是一個閏年,因此將數據分成訓練和測試集并不是一個干凈的 5 - 2 年分裂,但是對于本教程來說足夠接近。 ```py # load and prepare from pandas import read_csv from matplotlib import pyplot from numpy import array from numpy import hstack from numpy import savetxt # load dataset data = read_csv('eighthr.data', header=None, index_col=0, parse_dates=True, squeeze=True) values = data.values # replace missing observations with 0 values[values=='?'] = 0.0 # frame as supervised learning supervised = list() for i in range(len(values) - 1): X, y = values[i, :-1], values[i + 1, -1] row = hstack((X,y)) supervised.append(row) supervised = array(supervised) # split into train-test split = 365 * 2 train, test = supervised[:-split,:], supervised[-split:,:] train, test = train.astype('float32'), test.astype('float32') print(train.shape, test.shape) # save prepared datasets savetxt('train.csv', train, delimiter=',') savetxt('test.csv', test, delimiter=',') ``` 運行該示例將訓練和測試集保存為 CSV 文件,并匯總兩個數據集的形狀。 ```py (1803, 73) (730, 73) ``` ## 樸素預測模型 一個樸素的模型可以預測每天臭氧日的概率。 這是一種樸素的方法,因為它不使用除事件基本速率之外的任何信息。在氣象預報的驗證中,這被稱為氣候預報。 我們可以從訓練數據集中估計臭氧日的概率,如下所示。 ```py # load datasets train = loadtxt('train.csv', delimiter=',') test = loadtxt('test.csv', delimiter=',') # estimate naive probabilistic forecast naive = sum(train[:,-1]) / train.shape[0] ``` 然后,我們可以預測測試數據集中每天臭氧日的初始概率。 ```py # forecast the test dataset yhat = [naive for _ in range(len(test))] ``` 一旦我們有了預測,我們就可以對其進行評估。 評估概率預測的有用措施是 [Brier 評分](https://en.wikipedia.org/wiki/Brier_score)。該分數可以被認為是來自預期概率(0%或 1%)的預測概率(例如 5%)的均方誤差。它是測試數據集中每天發生的錯誤的平均值。 我們感興趣的是最小化 Brier 分數,較小的值更好,例如更小的錯誤。 我們可以使用 scikit-learn 庫中的 [brier_score_loss()函數](http://scikit-learn.org/stable/modules/generated/sklearn.metrics.brier_score_loss.html)來評估預測的 Brier 分數。 ```py # evaluate forecast testy = test[:, -1] bs = brier_score_loss(testy, yhat) print('Brier Score: %.6f' % bs) ``` 對于熟練的模型,它必須具有比樸素預測的分數更好的分數。 我們可以通過計算基于樸素預測標準化 Brier 分數(BS)的 Brier 技能分數(BSS)來證明這一點。 我們預計樸素預報的計算 BSS 將為 0.0。展望未來,我們有興趣最大化此分數,例如較大的 BSS 分數更好。 ```py # calculate brier skill score bs_ref = bs bss = (bs - bs_ref) / (0 - bs_ref) print('Brier Skill Score: %.6f' % bss) ``` 下面列出了幼稚預測的完整示例。 ```py # naive prediction method from sklearn.metrics import brier_score_loss from numpy import loadtxt # load datasets train = loadtxt('train.csv', delimiter=',') test = loadtxt('test.csv', delimiter=',') # estimate naive probabilistic forecast naive = sum(train[:,-1]) / train.shape[0] print(naive) # forecast the test dataset yhat = [naive for _ in range(len(test))] # evaluate forecast testy = test[:, -1] bs = brier_score_loss(testy, yhat) print('Brier Score: %.6f' % bs) # calculate brier skill score bs_ref = bs bss = (bs - bs_ref) / (0 - bs_ref) print('Brier Skill Score: %.6f' % bss) ``` 運行這個例子,我們可以看到臭氧日的樸素概率甚至約為 7.2%。 使用基本費率作為預測會導致 Brier 技能為 0.039,預期 Brier 技能得分為 0.0(忽略該符號)。 ```py 0.07265668330560178 Brier Score: 0.039232 Brier Skill Score: -0.000000 ``` 我們現在準備探索一些機器學習方法,看看我們是否可以為此預測添加技能。 請注意,原始論文使用精確度和直接召回來評估方法的技巧,這是一種用于方法之間直接比較的令人驚訝的方法。 也許您可以探索的替代措施是 ROC 曲線下的面積(ROC AUC)。繪制最終模型的 ROC 曲線將允許模型的操作者選擇閾值,該閾值提供真正的正(命中)和誤報(誤報)速率之間的期望平衡水平。 ## 集合樹預測模型 原始論文報告了袋裝決策樹的一些成功。 > 盡管我們對歸納學習器的選擇是非窮盡的,但本文已經表明,歸納學習可以作為臭氧水平預測的一種選擇方法,而基于集合的概率樹提供比現有方法更好的預測(更高的召回率和精確度)。 - [預測偏差隨機臭氧天數:分析和解決方案](https://ieeexplore.ieee.org/abstract/document/4053100/),2006 年。 出于以下幾個原因,這并不奇怪: * 袋裝決策樹不需要任何數據縮放。 * Bagged 決策樹自動執行一種功能部分,忽略不相關的功能。 * 袋裝決策樹預測合理校準的概率(例如,與 SVM 不同)。 這表明在測試機器學習算法時,這是一個很好的起點。 我們可以通過現場檢查 scikit-learn 庫中標準集合樹方法樣本的表現來快速入門,其默認配置和樹數設置為 100。 具體來說,方法: * 袋裝決策樹(BaggingClassifier) * 額外樹木(ExtraTreesClassifier) * 隨機梯度提升(GradientBoostingClassifier) * 隨機森林(RandomForestClassifier) 首先,我們必須將訓練和測試數據集分成輸入(X)和輸出(y)組件,以便我們可以擬合 sklearn 模型。 ```py # load datasets train = loadtxt('train.csv', delimiter=',') test = loadtxt('test.csv', delimiter=',') # split into inputs/outputs trainX, trainy, testX, testy = train[:,:-1],train[:,-1],test[:,:-1],test[:,-1] ``` 我們還需要 Brier 分數進行樸素的預測,以便我們能夠正確計算新模型的 Brier 技能分數。 ```py # estimate naive probabilistic forecast naive = sum(train[:,-1]) / train.shape[0] # forecast the test dataset yhat = [naive for _ in range(len(test))] # calculate naive bs bs_ref = brier_score_loss(testy, yhat) ``` 我們可以一般地評估單個 scikit-learn 模型的技能。 下面定義名為 _evaluate_once()_ 的函數,該函數適合并評估給定的已定義和配置的 scikit-learn 模型并返回 Brier 技能分數(BSS)。 ```py # evaluate a sklearn model def evaluate_once(bs_ref, template, trainX, trainy, testX, testy): # fit model model = clone(template) model.fit(trainX, trainy) # predict probabilities for 0 and 1 probs = model.predict_proba(testX) # keep the probabilities for class=1 only yhat = probs[:, 1] # calculate brier score bs = brier_score_loss(testy, yhat) # calculate brier skill score bss = (bs - bs_ref) / (0 - bs_ref) return bss ``` 集合樹是一種隨機機器學習方法。 這意味著當相同模型的相同配置在相同數據上訓練時,它們將做出不同的預測。為了糾正這個問題,我們可以多次評估給定模型,例如 10 次,并計算每次運行的平均技能。 下面的函數將評估給定模型 10 次,打印平均 BSS 分數,并返回分數的總體用于分析。 ```py # evaluate an sklearn model n times def evaluate(bs_ref, model, trainX, trainy, testX, testy, n=10): scores = [evaluate_once(bs_ref, model, trainX, trainy, testX, testy) for _ in range(n)] print('>%s, bss=%.6f' % (type(model), mean(scores))) return scores ``` 我們現在準備評估一套集合決策樹算法。 下面列出了完整的示例。 ```py # evaluate ensemble tree methods from numpy import loadtxt from numpy import mean from matplotlib import pyplot from sklearn.base import clone from sklearn.metrics import brier_score_loss from sklearn.ensemble import BaggingClassifier from sklearn.ensemble import ExtraTreesClassifier from sklearn.ensemble import GradientBoostingClassifier from sklearn.ensemble import RandomForestClassifier # evaluate a sklearn model def evaluate_once(bs_ref, template, trainX, trainy, testX, testy): # fit model model = clone(template) model.fit(trainX, trainy) # predict probabilities for 0 and 1 probs = model.predict_proba(testX) # keep the probabilities for class=1 only yhat = probs[:, 1] # calculate brier score bs = brier_score_loss(testy, yhat) # calculate brier skill score bss = (bs - bs_ref) / (0 - bs_ref) return bss # evaluate an sklearn model n times def evaluate(bs_ref, model, trainX, trainy, testX, testy, n=10): scores = [evaluate_once(bs_ref, model, trainX, trainy, testX, testy) for _ in range(n)] print('>%s, bss=%.6f' % (type(model), mean(scores))) return scores # load datasets train = loadtxt('train.csv', delimiter=',') test = loadtxt('test.csv', delimiter=',') # split into inputs/outputs trainX, trainy, testX, testy = train[:,:-1],train[:,-1],test[:,:-1],test[:,-1] # estimate naive probabilistic forecast naive = sum(train[:,-1]) / train.shape[0] # forecast the test dataset yhat = [naive for _ in range(len(test))] # calculate naive bs bs_ref = brier_score_loss(testy, yhat) # evaluate a suite of ensemble tree methods scores, names = list(), list() n_trees=100 # bagging model = BaggingClassifier(n_estimators=n_trees) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('bagging') # extra model = ExtraTreesClassifier(n_estimators=n_trees) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('extra') # gbm model = GradientBoostingClassifier(n_estimators=n_trees) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('gbm') # rf model = RandomForestClassifier(n_estimators=n_trees) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('rf') # plot results pyplot.boxplot(scores, labels=names) pyplot.show() ``` 運行該示例總結了 10 次運行中平均每個模型的平均 BSS。 鑒于算法的隨機性,您的具體結果可能會有所不同,但趨勢應該相同。 從平均 BSS 分數來看,它表明額外的樹木,隨機梯度增強和隨機森林模型是最熟練的。 ```py ><class 'sklearn.ensemble.bagging.BaggingClassifier'>, bss=0.069762 ><class 'sklearn.ensemble.forest.ExtraTreesClassifier'>, bss=0.103291 ><class 'sklearn.ensemble.gradient_boosting.GradientBoostingClassifier'>, bss=0.119803 ><class 'sklearn.ensemble.forest.RandomForestClassifier'>, bss=0.102736 ``` 繪制每個模型的分數的盒子和須狀圖。 他們所有跑步的所有模型都顯示出樸素預測的技巧(正分數),這是非常令人鼓舞的。 額外樹木,隨機梯度提升和隨機森林的 BSS 分數的分布看起來都令人鼓舞。 ![Box and whisker plot of ensemble decision tree BSS scores on the test set](https://img.kancloud.cn/6d/91/6d9134df6454b3ea5cc6aa3d7415974b_1280x960.jpg) 測試集上的集合決策樹 BSS 分數的框和胡須圖 ## 調整梯度提升 鑒于隨機梯度增強看起來很有希望,有必要探討是否可以通過某些[參數調整](http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.GradientBoostingClassifier.html)進一步提升模型的表現。 有許多參數可以調整模型,但調整模型的一些好的啟發式方法包括: * 降低學習率( _learning_rate_ ),同時增加決策樹的數量( _n_estimators_ )。 * 增加決策樹的最大深度( _max_depth_ ),同時減少可用于擬合樹的樣本數(_ 樣本 _)。 我們可以根據這些原則檢查一些參數,而不是網格搜索值。如果您有時間和計算資源,可以自己探索這些參數的網格搜索。 我們將比較 GBM 模型的四種配置: * **基線**:如前一節測試的那樣( _learning_rate_ = 0.1, _n_estimators_ = 100,_ 子樣本 _ = 1.0, _max_depth_ = 3) * **lr** ,學習率較低且樹木較多( _learning_rate_ = 0.01, _n_estimators_ = 500,_ 子樣本 _ = 1.0, _max_depth_ = 3) * **深度**,最大樹深度增加,數據集采樣量減少( _learning_rate_ = 0.1, _n_estimators_ = 100,_ 子樣本 _ = 0.7, _max_depth_ =) * **所有**,所有修改。 下面列出了完整的示例。 ```py # tune the gbm configuration from numpy import loadtxt from numpy import mean from matplotlib import pyplot from sklearn.base import clone from sklearn.metrics import brier_score_loss from sklearn.ensemble import BaggingClassifier from sklearn.ensemble import ExtraTreesClassifier from sklearn.ensemble import GradientBoostingClassifier from sklearn.ensemble import RandomForestClassifier # evaluate a sklearn model def evaluate_once(bs_ref, template, trainX, trainy, testX, testy): # fit model model = clone(template) model.fit(trainX, trainy) # predict probabilities for 0 and 1 probs = model.predict_proba(testX) # keep the probabilities for class=1 only yhat = probs[:, 1] # calculate brier score bs = brier_score_loss(testy, yhat) # calculate brier skill score bss = (bs - bs_ref) / (0 - bs_ref) return bss # evaluate an sklearn model n times def evaluate(bs_ref, model, trainX, trainy, testX, testy, n=10): scores = [evaluate_once(bs_ref, model, trainX, trainy, testX, testy) for _ in range(n)] print('>%s, bss=%.6f' % (type(model), mean(scores))) return scores # load datasets train = loadtxt('train.csv', delimiter=',') test = loadtxt('test.csv', delimiter=',') # split into inputs/outputs trainX, trainy, testX, testy = train[:,:-1],train[:,-1],test[:,:-1],test[:,-1] # estimate naive probabilistic forecast naive = sum(train[:,-1]) / train.shape[0] # forecast the test dataset yhat = [naive for _ in range(len(test))] # calculate naive bs bs_ref = brier_score_loss(testy, yhat) # evaluate a suite of ensemble tree methods scores, names = list(), list() # base model = GradientBoostingClassifier(learning_rate=0.1, n_estimators=100, subsample=1.0, max_depth=3) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('base') # learning rate model = GradientBoostingClassifier(learning_rate=0.01, n_estimators=500, subsample=1.0, max_depth=3) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('lr') # depth model = GradientBoostingClassifier(learning_rate=0.1, n_estimators=100, subsample=0.7, max_depth=7) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('depth') # all model = GradientBoostingClassifier(learning_rate=0.01, n_estimators=500, subsample=0.7, max_depth=7) avg_bss = evaluate(bs_ref, model, trainX, trainy, testX, testy) scores.append(avg_bss) names.append('all') # plot results pyplot.boxplot(scores, labels=names) pyplot.show() ``` 運行該示例為每個配置打印 10 個運行的平均每個模型的 BSS。 結果表明,單獨學習率和樹木數量的變化引起了對默認配置的一些提升。 結果還表明,包含每個變化的“所有”配置導致最佳平均 BSS。 ```py ><class 'sklearn.ensemble.gradient_boosting.GradientBoostingClassifier'>, bss=0.119972 ><class 'sklearn.ensemble.gradient_boosting.GradientBoostingClassifier'>, bss=0.145596 ><class 'sklearn.ensemble.gradient_boosting.GradientBoostingClassifier'>, bss=0.095871 ><class 'sklearn.ensemble.gradient_boosting.GradientBoostingClassifier'>, bss=0.192175 ``` 創建來自每個配置的 BSS 分數的框和胡須圖。我們可以看到包含所有更改的配置明顯優于基線模型和其他配置組合。 也許通過對模型進行微調的參數可以獲得進一步的收益。 ![Box and whisker plot of tuned GBM models showing BSS scores on the test set](https://img.kancloud.cn/4f/68/4f68856196a3ee9e5e0ec2ceb5466544_1280x960.jpg) 調諧 GBM 模型的框和胡須圖顯示測試集上的 BSS 分數 有必要掌握論文中描述的參數模型以及使用它所需的數據,以便將其技能與最終模型的技能進行比較。 ## 擴展 本節列出了一些擴展您可能希望探索的教程的想法。 * 探索使用前幾天觀察結果的模型框架。 * 探索使用 ROC 曲線圖和 ROC AUC 測量的模型評估。 * 網格搜索梯度提升模型參數,并可能探索其他實現,如 XGBoost。 如果你探索任何這些擴展,我很想知道。 ## 進一步閱讀 如果您希望深入了解,本節將提供有關該主題的更多資源。 * [臭氧水平檢測數據集,UCI 機器學習庫](https://archive.ics.uci.edu/ml/datasets/ozone+level+detection)。 * [預測偏差隨機臭氧天數:分析和解決方案](https://ieeexplore.ieee.org/abstract/document/4053100/),2006 年。 * [預測有偏差的隨機臭氧天數:分析,解決方案以及](https://link.springer.com/article/10.1007/s10115-007-0095-1),2008 年。 * [CAWCR 驗證頁面](http://www.cawcr.gov.au/projects/verification/) * 維基百科上的[接收器操作特性](https://en.wikipedia.org/wiki/Receiver_operating_characteristic) ## 摘要 在本教程中,您了解了如何開發概率預測模型來預測德克薩斯州休斯頓的空氣污染。 具體來說,你學到了: * 如何加載和準備臭氧日標準機器學習預測建模問題。 * 如何開發一個樸素的預測模型,并使用 Brier 技能分數評估預測。 * 如何使用決策樹集合開發技巧模型,并通過成功模型的超參數調整進一步提高技能。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看