13.Data Leakage
本教程是ML系列的一部分。在此步驟中,你將學習什么是data leakage及如何預防它。?
What is Data Leakage
數據泄漏是數據科學家需要理解的最重要問題之一。 如果您不知道如何防止它,則會頻繁出現泄漏,并且會以最微妙和危險的方式破壞您的模型。 具體而言,泄漏會導致模型看起來準確,當您開始使用模型做出決策,模型則變得非常不準確。 本教程將向您展示泄漏是什么以及如何避免泄漏。
泄漏有兩種主要類型:Leaky Predictors and a Leaky Validation Strategies。
Leaky Predictors
當您的預測因素包含無法使用的數據時,就會發生這種情況。
例如,假設您想預測誰會患上肺炎。 原始數據的前幾行可能如下所示:
| False | 65 | 100 | False | False | ... |
| False | 72 | 130 | True | False | ... |
| True | 58 | 100 | False | True | ... |
人們在患肺炎后服用抗生素藥物才能康復。 因此原始數據顯示了這些列之間的緊密關系。 但是,確定了got_pneumonia的值后,take_antibiotic_medicine經常被改變。 這是目標泄漏。
該模型將發現,對于take_antibiotic_medicine而言,任何具有False值的人都沒有肺炎。 驗證數據來自同一來源,因此模式將在驗證中重復,模型將具有很好的驗證(或交叉驗證)分數。 但隨后在現實世界中部署時,該模型將非常不準確。
為防止此類數據泄漏,應排除在目標值實現后更新(或創建)的任何變量。 因為當我們使用此模型進行新的預測時,該數據將無法使用。
Leaky Validation Strategy
當您不小心區分訓練數據和驗證數據時,會發生不同類型的泄漏。 例如,如果在調用train_test_split之前運行預處理(比如為缺失值擬合Imputer),就會發生這種情況。 驗證旨在衡量模型對之前未考慮過的數據的影響。 如果驗證數據影響預處理行為,您可以以微妙的方式破壞此過程。最終結果? 您的模型將獲得非常好的驗證分數,讓您對它充滿信心,但在部署它以做出決策時表現不佳。
Preventing Leaky Predictors
沒有一種解決方案可以普遍地防止泄漏的預測因素。 它需要有關您的數據,特定案例檢查和常識的知識。
然而,泄漏預測因素通常與目標具有高度統計相關性。 所以要記住兩個策略:
- ???? 要篩選可能的泄漏預測因素,請查找與目標統計相關的列。
- ???? 如果您構建模型并發現它非常準確,則可能存在泄漏問題。
Preventing Leaky Validation Strategies
如果您的驗證基于簡單的train-test-split,則從任何類型的擬合中排除驗證數據,包括預處理步驟的擬合。 如果您使用scikit-learn Pipelines,這會更容易。 使用交叉驗證時,使用管道并在管道內進行預處理更為重要。
Example
我們將使用一個關于信用卡應用程序的小數據集,我們將構建一個模型來預測哪些應用程序被接受(存儲在一個名為card的變量中)。 以下是數據:
[1]
import pandas as pddata = pd.read_csv('../input/AER_credit_card_data.csv', true_values = ['yes'],false_values = ['no']) print(data.head()) card reports age income share expenditure owner selfemp \ 0 True 0 37.66667 4.5200 0.033270 124.983300 True False 1 True 0 33.25000 2.4200 0.005217 9.854167 False False 2 True 0 33.66667 4.5000 0.004156 15.000000 True False 3 True 0 30.50000 2.5400 0.065214 137.869200 False False 4 True 0 32.16667 9.7867 0.067051 546.503300 True False dependents months majorcards active 0 3 54 1 12 1 3 34 1 13 2 4 58 1 5 3 0 25 1 7 4 2 64 1 5我們使用data.shape看到這是一個小數據集(1312行),所以我們應該使用交叉驗證來確保模型質量的準確精度。
[2]
data.shape(1319, 12)[3]
from sklearn.pipeline import make_pipeline from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import cross_val_scorey = data.card X = data.drop(['card'], axis=1)# Since there was no preprocessing, we didn't need a pipeline here. Used anyway as best practice modeling_pipeline = make_pipeline(RandomForestClassifier()) cv_scores = cross_val_score(modeling_pipeline, X, y, scoring='accuracy') print("Cross-val accuracy: %f" %cv_scores.mean())Cross-val accuracy: 0.979528根據經驗,您會發現找到98%的精確模型是非常罕見的。它發生了,但我們應該更仔細地檢查數據以確定它是否是目標泄漏。
以下是數據摘要,您也可以在數據選項卡下找到它:
- card: Dummy variable, 1 if application for credit card accepted, 0 if not
- reports: Number of major derogatory reports
- age: Age n years plus twelfths of a year
- income: Yearly income (divided by 10,000)
- share: Ratio of monthly credit card expenditure to yearly income
- expenditure: Average monthly credit card expenditure
- owner: 1 if owns their home, 0 if rent
- selfempl: 1 if self employed, 0 if not.
- dependents: 1 + number of dependents
- months: Months living at current address
- majorcards: Number of major credit cards held
- active: Number of active credit accounts
一些變量看起來很可疑。例如,expenditure是指支付此卡還是在使用之前卡上的支出?
此時,基本數據比較可能非常有用:
[4]
expenditures_cardholders = data.expenditure[data.card] expenditures_noncardholders = data.expenditure[~data.card]print('Fraction of those who received a card with no expenditures: %.2f' \%(( expenditures_cardholders == 0).mean())) print('Fraction of those who received a card with no expenditures: %.2f' \%((expenditures_noncardholders == 0).mean())) Fraction of those who received a card with no expenditures: 0.02 Fraction of those who received a card with no expenditures: 1.00每個人(card == False)都沒有支出,而card== True的人中只有2%沒有支出。 我們的模型似乎具有高精度并不奇怪。 但這似乎是數據泄露,其中支出可能意味著*他們申請的卡上的支出。**。
由于share部分由支出決定,因此也應予以排除。 變量active,majorcards有點不太清楚,但從描述中看,它們聽起來很有意義。 在大多數情況下,如果您無法追蹤創建數據的人員以了解更多信息,那么最好是安全而不是抱歉。
我們將運行一個沒有泄漏的模型如下:
[5]
potential_leaks = ['expenditure', 'share', 'active', 'majorcards'] X2 = X.drop(potential_leaks, axis=1) cv_scores = cross_val_score(modeling_pipeline, X2, y, scoring='accuracy') print("Cross-val accuracy: %f" %cv_scores.mean())Cross-val accuracy: 0.806677這種準確性相當低,這一方面令人失望。 但是,我們可以預期在新應用程序中使用它的時間大約為80%,而泄漏模型可能會比這更糟糕(即使它在交叉驗證中的表觀得分更高)。
Conlusion
在許多數據科學應用中,數據泄漏可能是數百萬美元的錯誤。 仔細分離訓練和驗證數據是第一步,管道可以幫助實現這種分離。 泄漏預測因素是一個更常見的問題,泄漏的預測因子更難以追蹤。 謹慎,常識和數據探索的組合可以幫助識別泄漏的預測變量,以便從模型中刪除它們。
Exercise
查看正在進行的項目中的數據。 有沒有可能導致泄漏的預測因子? 作為提示,來自Kaggle比賽的大多數數據集都沒有這些變量。 一旦你通過那些精心策劃的數據集,這就成了一個常見的問題。
總結
以上是生活随笔為你收集整理的13.Data Leakage的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ofps.exe - ofps是什么进程
- 下一篇: 百度顶会论文复现(3):视频分类综述