What is Data Leakage

Data leakage is one of the most important issues for a data scientist to understand. If you don't know how to prevent it, leakage will come up frequently, and it will ruin your models in the most subtle and dangerous ways. Specifically, leakage causes a model to look accurate until you start making decisions with the model, and then the model becomes very inaccurate. This tutorial will show you what leakage is and how to avoid it.

There are two main types of leakage: Leaky Predictors and a Leaky Validation Strategies.

Leaky Predictors

This occurs when your predictors include data that will not be available at the time you make predictions.

For example, imagine you want to predict who will get sick with pneumonia. The top few rows of your raw data might look like this:

got_pneumonia age weight male took_antibiotic_medicine ...
False 65 100 False False ...
False 72 130 True False ...
True 58 100 False True ...

-

People take antibiotic medicines after getting pneumonia in order to recover. So the raw data shows a strong relationship between those columns. But took_antibiotic_medicine is frequently changed after the value for got_pneumonia is determined. This is target leakage.

The model would see that anyone who has a value of False for took_antibiotic_medicine didn't have pneumonia. Validation data comes from the same source, so the pattern will repeat itself in validation, and the model will have great validation (or cross-validation) scores. But the model will be very inaccurate when subsequently deployed in the real world.

To prevent this type of data leakage, any variable updated (or created) after the target value is realized should be excluded. Because when we use this model to make new predictions, that data won't be available to the model.

Leaky Validation Strategy

A much different type of leak occurs when you aren't careful distinguishing training data from validation data. For example, this happens if you run preprocessing (like fitting the Imputer for missing values) before calling train_test_split. Validation is meant to be a measure of how the model does on data it hasn't considered before. You can corrupt this process in subtle ways if the validation data affects the preprocessing behavoir.. The end result? Your model will get very good validation scores, giving you great confidence in it, but perform poorly when you deploy it to make decisions.

Preventing Leaky Predictors

There is no single solution that universally prevents leaky predictors. It requires knowledge about your data, case-specific inspection and common sense.

However, leaky predictors frequently have high statistical correlations to the target. So two tactics to keep in mind:

  • To screen for possible leaky predictors, look for columns that are statistically correlated to your target.
  • If you build a model and find it extremely accurate, you likely have a leakage problem.

Preventing Leaky Validation Strategies

If your validation is based on a simple train-test split, exclude the validation data from any type of fitting, including the fitting of preprocessing steps. This is easier if you use scikit-learn Pipelines. When using cross-validation, it's even more critical that you use pipelines and do your preprocessing inside the pipeline.

Example

We will use a small dataset about credit card applications, and we will build a model predicting which applications were accepted (stored in a variable called card). Here is a look at the data:

 
import pandas as pd

data = 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
 

We can see with data.shape that this is a small dataset (1312 rows), so we should use cross-validation to ensure accurate measures of model quality

 
data.shape
 
(1319, 12)
 
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score y = 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
 

With experience, you'll find that it's very rare to find models that are accurate 98% of the time. It happens, but it's rare enough that we should inspect the data more closely to see if it is target leakage.

Here is a summary of the data, which you can also find under the data tab:

  • 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

A few variables look suspicious. For example, does expenditure mean expenditure on this card or on cards used before appying?

At this point, basic data comparisons can be very helpful:

 
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
 

Everyone with card == False had no expenditures, while only 2% of those with card == True had no expenditures. It's not surprising that our model appeared to have a high accuracy. But this seems a data leak, where expenditures probably means *expenditures on the card they applied for.**.

Since share is partially determined by expenditure, it should be excluded too. The variables active, majorcards are a little less clear, but from the description, they sound concerning. In most situations, it's better to be safe than sorry if you can't track down the people who created the data to find out more.

We would run a model without leakage as follows:

 
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
 

This accuracy is quite a bit lower, which on the one hand is disappointing. However, we can expect it to be right about 80% of the time when used on new applications, whereas the leaky model would likely do much worse then that (even in spite of it's higher apparent score in cross-validation.).

Conclusion

Data leakage can be multi-million dollar mistake in many data science applications. Careful separation of training and validation data is a first step, and pipelines can help implement this separation. Leaking predictors are a more frequent issue, and leaking predictors are harder to track down. A combination of caution, common sense and data exploration can help identify leaking predictors so you remove them from your model.

有极高的精准度,代表可能发生数据泄露。

Data Leakage是因果关系的纰漏,是由于数据准备过程中出现的失误,使模型沿着有纰漏的,甚至是颠倒的因果关系进行预测,但得到极好的预测结果。

按我的理解就是,用于训练X中的某个或某几个特征与最终要预测的y有强烈的因果关系,预测后这些特征的权重很大(对应的theta值),导致模型最终由这几个特诊所主导。

Data Leakage 基本都是在准备数据的时候,或者数据采样的时候出了问题,误将与结果直接相关的feature纳入了数据集。这样的纰漏,比较难以发现。

处理方法是丢掉这些特征列。

kaggle Data Leakage的更多相关文章

  1. Data Leakage 因果性

    参考这篇: https://blog.csdn.net/jiandanjinxin/article/details/54633475 再论数据科学竞赛中的Data Leakage 存在和利用这种倒‘因 ...

  2. Data Leakage in Machine Learning 机器学习训练中的数据泄漏

    refer to:  https://www.kaggle.com/dansbecker/data-leakage There are two main types of leakage: Leaky ...

  3. 贝叶斯分类器,随机森林,梯度下载森林,神经网络相关参数的意义和data leakage

    构建的每一颗树的数据都是有放回的随机抽取的(也叫bootstrap),n_estimators参数是你想设置多少颗树,还有就是在进行树的结点分裂的时候,是随机选取一个特征子集,然后找到最佳的分裂标准.

  4. Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?

    Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...

  5. DeepLearning to digit recognizer in kaggle

    DeepLearning to digit recongnizer in kaggle 近期在看deeplearning,于是就找了kaggle上字符识别进行练习.这里我主要用两种工具箱进行求解.并比 ...

  6. Enabling granular discretionary access control for data stored in a cloud computing environment

    Enabling discretionary data access control in a cloud computing environment can begin with the obtai ...

  7. Common Pitfalls In Machine Learning Projects

    Common Pitfalls In Machine Learning Projects In a recent presentation, Ben Hamner described the comm ...

  8. How do I learn machine learning?

    https://www.quora.com/How-do-I-learn-machine-learning-1?redirected_qid=6578644   How Can I Learn X? ...

  9. python 数据分析--词云图,图形可视化美国竞选辩论

    这篇博客从用python实现分析数据的一个完整过程.以下着重几个python的moudle的运用"pandas",""wordcloud"," ...

随机推荐

  1. Technocup 2019 C. Compress String

    一个字符串 $s$,你要把它分成若干段,有两种合法的段 1.段长为 $1$,代价为 $a$ 2.这个段是前面所有段拼起来组成的字符串的字串,代价为 $b$ 问最小代价 $|s| \leq 5000$ ...

  2. HihoCoder1449 重复旋律6(后缀自动机)

    描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数列. 现在小Hi想知道一部作品中所有长度为K的旋律中出现次数最多的旋律的出现次数.但是K不是固定的,小Hi想知道对 ...

  3. linux使用收集

    Centos7 命令 # 查询正运行的java进程,建议使用jps,使用ps会将tail也显示出来 jps -lvm | grep '/home/chencye/tomcat/apache-tomca ...

  4. (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

    (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows Shell 外壳编程系列3 - 上下文菜单(iContextMenu)(一)右键菜单 上一节说到如 ...

  5. 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT

    题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...

  6. 5、Selenium+Python自动登录163邮箱发送邮件

    1.Selenium实现自动化,需要定位元素,以下查看163邮箱的登录元素 (1)登录(定位到登录框,登录框是一个iframe,如果没有定位到iframe,是无法定位到账号框与密码框) 定位到邮箱框( ...

  7. 用Json Template在Azure上创建Cisco CSR路由器

    Azure的ARM模式可以通过Json的模板创建VM.本文以Cisco的CSR的image为例,介绍如何用Json的创建VM. 一.Cisco CSR的Image 首先把Cisco CSR的image ...

  8. (转)基于PHP——简单的WSDL的创建(WSDL篇)

    本文转载自:http://blog.csdn.net/rrr4578/article/details/24451943 1.建立WSDL文件     建立WSDL的工具很多,eclipse.zends ...

  9. Java 的编译和运行机制

    创建一个 名为 test.java 的 Java 源文件 源代码: class Hello{ public static void main(String[] args) { System.out.p ...

  10. 在Google Colab中导入一个本地模块或.py文件

    模块与单个.py文件的区别,模块中含有__init__.py文件,其中函数调用使用的是相对路径,如果使用导入.py文件的方法在Google Colab中导入模块 会报错:Attempted relat ...