AI 能在模型达到 99% 准确率前发现数据泄漏吗

4 种经典的数据泄漏,植入真实数据中,在 4 个模型上进行冷测试。
作者:Nate Rosidi,KDnuggets 市场趋势与 SQL 内容专家,2026 年 11 月 9 日,机器学习
每位数据科学家都熟悉这种感觉:模型返回 99% 的准确率,我们非但不会庆祝,反而会心生疑虑。十有八九,原因是数据泄漏:训练数据中的某些信息,在预测时实际上根本无法获得。
我们想要两个真实的数字。第一,在真实数据而非玩具示例上,这种准确率的跃升有多夸张?第二,如果在任何人运行之前,我们就把整个流程描述给一个 AI 模型,且不提示任何地方有问题,它能否发现这个泄漏?答案是否会因我们询问的模型不同而改变?
于是我们选取了四个真实数据项目,给每一个植入一种不同的、广为人知的泄漏模式,分别在存在泄漏和不存在泄漏的情况下训练真实模型,并记录下实际数字。
随后,我们单独用平实的语言描述每个流程,并询问四款 Claude 模型——从最快到最强——在相信结果之前,它们会提出哪些疑点。
实验设置
每种泄漏进入流程的路径各不相同:一个从机制上直接定义标签的特征、一个用未来信息构建的特征、一次忽略关系结构的数据划分,以及一个顺序做错的数据预处理步骤。
在 AI 方面,我们测试了 Claude Haiku 4.5、Claude Sonnet 5、Claude Opus 5 和 Claude Fable 5.1,覆盖了我们所能获取的速度与能力范围。
我们明确列出了所测试的具体模型,并不暗示这是跨厂商对比,因为我们并没有做那样的测试。每个模型都获得一个全新会话,对其他模型毫无记忆,只得到关于流水线、特征或代码的闭卷式描述,且无法运行任何内容。没有任何模型被告知存在泄漏。
案例 1:早已知道答案的特征
在本案例中,我们使用的是来自“股价方向预测”(Prediction of Stock Price Direction)项目的数据集。
任务:预测亚马逊的收盘价是否高于当天开盘价。此处,若 Close 大于 Open,则 Label 为 1,否则为 0。
以下是其中一个数据集的一个样本,此处为 AMZN_train.csv:
对比存在泄漏与无泄漏时的表现
把当天的 Close 和 Adj Close 与 Open、High、Low、Volume 一起输入模型,验证准确率达到 97.0%,曲线下面积(AUC)为 0.999。
如果只使用前一日的开盘价、最高价、最低价、收盘价和成交量,完全不含被预测当日的任何数据,准确率就会跌到 51.6%,AUC 0.476:跟抛硬币没什么两样。97% 对 52%,这一差距用一个数字就概括了整篇文章。
四个模型都立刻抓住了这一点,而且理由正确:收盘价(Close)本身就定义了这个标签。
Haiku 称其为「循环论证」。
Sonnet 表示,该模型「只需一次比较就能重建标签」。
Opus 更进一步,无需提示便指出:「如果最高价等于开盘价,那么当天价格从未高于开盘价,所以标签必然是 0。」它还自行给出了诚实的预期:一个公允的模型准确率应该落在「50% 出头」,而这几乎正是我们干净运行所得到的结果。
案例 2:已经告诉你界线在哪里的数据集
在本案例中,我们使用的是 Delivery Duration Prediction 项目的数据集,具体取其数据集 historical_data.csv 的一个子集。
任务:预测 duration_sec,即订单从下单到送达之间的秒数,计算方式为 actual_delivery_time 减去 created_at。
DoorDash 的 historical_data.csv 包含 197,428 条订单,每行一条,共 16 列。目标变量是 duration_sec:actual_delivery_time 减去 created_at,即从下单到送达的秒数。
模型实际看到的列如下:
我们排除了 market_id、store_primary_category 和 order_protocol:分别对应地区、餐厅品类和下单渠道。不是因为它们没用,而是因为对 74 种不同的餐厅品类做编码,与我们正在测试的泄漏问题毫无关系。
我们添加了一个特征工程生成的变量——门店的平均配送时长,用两种方式计算:
# 泄漏版:使用每一行,包括那些即将被评分的行 store_avg = df.groupby("store_id")["duration_sec"].transform("mean") df_shuffled = df.sample(frac=1.0) # 对带时间戳的数据做随机划分 # 干净版:只用训练期的订单,然后按时间顺序划分 store_avg = train.groupby("store_id")["duration_sec"].mean() train["store_avg"] = train["store_id"].map(store_avg) test["store_avg"] = test["store_id"].map(store_avg)
这一个特征工程变量就带来了两处独立的泄漏。
第一处:store_avg 的构造方式是把整张表——训练行和测试行放在一起——按 store_id 分组,对 duration_sec 取平均,而 duration_sec 正是我们要预测的目标,然后把这个平均值复制到每一行上。
对于只有寥寥几笔订单的门店,这个平均值会接近该行自身的 duration_sec,于是模型读到的其实是答案的一份平滑副本。
第二:created_at 是时间戳,因此这张表本身就按时间排序,在 80/20 划分之前将其打乱,会让训练行在时间上落在测试行之后。真实的生产模型只会看到在它被要求预测的那一单之前下的订单。
对比存在泄漏与不存在泄漏时的性能
我们用均方根误差(RMSE)为两个版本打分,单位为秒,与 duration_sec 相同。它回答的是“典型预测偏差有多大”,偏差大的样本会被赋予更高的权重。作为参照,这里订单的平均配送时长约为 47 分钟(中位数 44),因此 14 到 16 分钟的 RMSE 意味着这个诚实模型的典型偏差接近平均配送时长的三分之一。
泄漏版本报告的 RMSE 为 862.7 秒,而诚实版本为 949.9 秒,表面上提升了 9%,但这一提升完全来自泄漏,而不是模型学到了任何真实的东西。
所有模型都发现了门店均值泄漏。四个模型中只有三个发现了第二个问题:打乱一张带时间戳的表,而不是按时间顺序进行划分。Sonnet、Opus 和 Fable 都主动指出了这一点。Fable 的表述最清晰:“训练集可能包含发生在部分测试集订单之后的订单。”
Haiku 在修复了门店均值特征之后就停下了,从未提及划分问题。只有 Opus 补充说,DoorDash 自己的数据中已经包含一个值得超越的基线,即 estimated_order_place_duration 加上 estimated_store_to_consumer_driving_duration,而没有这一对比的 RMSE 说明不了什么。读者可以自行复现这一案例。
案例 3:无法从单个列中看出的泄漏
在本案例中,我们使用的是项目 Fraudsters Detection(Revolut)的数据集。
任务:根据用户画像和交易历史,预测每个用户的 IS_FRAUDSTER。
Revolut 的数据模式天然是一对多:一张 users 表带有一个标签 IS_FRAUDSTER,以及一张 transactions 表,每个用户对应多行。将它们连接后,一个用户因此可能出现在许多交易行中,且所有行都带有相同的欺诈标签。
预测问题本质上是要识别欺诈用户,尽管模型是在交易级别的行上训练的。
如果随机划分连接后的交易行,2,246 个用户中有 1,950 个会在划分的两侧都有交易。
这意味着测试集经常包含模型在训练期间已经见过的某个用户的另一笔交易。
比较泄漏与非泄漏性能
以这种方式训练的模型取得了 AUC 0.960,AUC 越高意味着欺诈者与非欺诈者之间的区分度越好。改为按用户划分,这样没有任何人的交易跨越划分边界,真实的 AUC 是 0.898:这是一个扎实的模型,只是并非所报告的那个。
所有四个模型都未经提示地指出了确切机制:标签存在于用户级别,因此按行划分会让模型部分识别出它已经见过的用户,而不是检测新用户中的欺诈。
Opus 的回答最为完整,它指出这里真正的样本量是用户数量,而不是交易数量,并且任何只有在欺诈调查结束后才会被填充的字段,无论怎样划分数据集,它本身都是一个独立的泄漏源。
我们在构建这个实验时也遇到了同样的问题:Revolut 的账户状态字段在数据中对于每一个非欺诈者都是 ACTIVE,而对于每一个欺诈者都是 LOCKED,这个特征的泄漏程度如此之高,以至于我们在动其他任何东西之前就把它删掉了。
案例 4:根本不在数据中的泄漏
在这个案例中,我们使用的是 Flagging Loans(Chime)项目的数据集。
任务是:根据 dpd_30_cnt、past_billings_cnt 以及两个信用评分等列,预测 bad_flag,即一笔贷款是否会变坏。
Chime 的贷款数据是不平衡的:约 10% 的贷款被标记为 bad_flag = 1,包含 dpd_30_cnt、past_billings_cnt 以及两个信用评分等列。为补偿这种不平衡,该流程在训练前会复制少数类别的行。
我们测试了一小段代码,没有数据,没有附带任何指标,只有代码……
X_bal, y_bal = oversample(X, y) # 先复制少数类别的行 X_train, X_test, y_train, y_test = train_test_split( X_bal, y_bal, stratify=y_bal )
原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。
查看英文原文(Can AI Spot Data Leakage Before Your Model Hits 99% Accuracy?)
4 classic leaks, planted in real data, tested cold on 4 models.
By Nate Rosidi, KDnuggets Market Trends & SQL Content Specialist on November 9, 2026 in Machine Learning

Every data scientist knows the feeling: a model comes back with 99% accuracy, and instead of celebrating, we get suspicious. Nine times out of ten, the cause is leakage: some piece of information in the training data that would not actually be available at prediction time.
We wanted two real numbers. First, how dramatic is that accuracy jump on real data, not a toy example? Second, if we describe the pipeline to an AI model before anyone runs it, with no hint that anything is wrong, does it catch the leak, and does the answer change depending on which model we ask?
So we took four real data projects, planted a different, well-known leakage pattern into each one, trained a real model with and without the leak, and recorded the actual numbers.
Then, separately, we described each pipeline in plain terms and asked four Claude models, from fastest to strongest, what they would flag before trusting the result.
Setting Up the Experiment
Each leak takes a different route into the pipeline: a feature that mechanically defines the label, a feature built with future information, a split that ignores relational structure, and a preprocessing step done in the wrong order.
For the AI side, we tested Claude Haiku 4.5, Claude Sonnet 5, Claude Opus 5, and Claude Fable 5.1, across the speed and capability range we have access to.
We are naming exactly which models we tested and not implying a cross-vendor comparison, since that is not what we ran. Each model got a fresh session with zero memory of the others, a closed-book description of the pipeline, features, or code, and no ability to run anything. None were told a leak existed.
Case 1: The Feature That Already Knows the Answer
For this case, we are using datasets from the project Prediction of Stock Price Direction.
The task: predict whether Amazon's closing price lands higher than that same day's opening price. Here, Label equals 1 if Close is greater than Open, 0 otherwise.
Here is a sample of one of the datasets, in this case AMZN_train.csv:
Comparing Leakage and Non-Leakage Performance
Feed the model that same day's Close and Adj Close alongside Open, High, Low, and Volume, and validation accuracy hits 97.0%, with an area under the curve (AUC) of 0.999.
Restrict it to only the prior day's open, high, low, close, and volume, nothing from the day being predicted, and accuracy falls to 51.6%, AUC 0.476: a coin flip. That difference, 97% versus 52%, is the whole article in one number.
All four models caught it instantly, for the right reason: Close defines the label.
Haiku called it "circular."
Sonnet said the model "can reconstruct the label with a single comparison."
Opus went further, unprompted: "if High equals Open, the day never traded above the open, so the label must be 0." It also set the honest expectation itself, that a fair model should land "in the low 50s for accuracy," almost exactly what our clean run produced.
Case 2: The Dataset That Already Told You Where the Line Is
For this case, we are using datasets from the project Delivery Duration Prediction. We are using a subset of its dataset, historical_data.csv.
The task: predict duration_sec, the seconds between an order being placed and delivered, computed as actual_delivery_time minus created_at.
DoorDash's historical_data.csv holds 197,428 orders, one row each, 16 columns. The target is duration_sec: actual_delivery_time minus created_at, the seconds from when the order was placed to when it arrived.
The columns the model actually saw:
We left out market_id, store_primary_category, and order_protocol: region, restaurant category, and order channel. Not because they're useless, just because encoding 74 different restaurant categories had nothing to do with the leak we were testing.
We added one engineered feature, a store's average delivery duration, computed in two ways:
# leaked: uses every row, including the ones about to be scored
store_avg = df.groupby("store_id")["duration_sec"].transform("mean")
df_shuffled = df.sample(frac=1.0) # random split on timestamped data
# clean: only training-period orders, then a chronological split
store_avg = train.groupby("store_id")["duration_sec"].mean()
train["store_avg"] = train["store_id"].map(store_avg)
test["store_avg"] = test["store_id"].map(store_avg)
That one engineered feature carries two separate leaks.
First: store_avg is built by grouping the whole table, train and test rows together, by store_id and averaging duration_sec, the exact thing we're predicting, then copying that average onto every row.
For a store with only a handful of orders, the average sits close to that row's own duration_sec, so the model is reading a smoothed copy of the answer.
Second: created_at is a timestamp, so this table is inherently ordered in time, and shuffling it before the 80/20 split lets training rows fall chronologically after test rows. A real production model only ever sees orders placed before the one it is asked to predict.
Comparing Leakage and Non-Leakage Performance
We scored both versions with root mean squared error (RMSE), in seconds, the same unit as duration_sec. It answers "how far off is a typical prediction," with large misses counted more heavily. For scale, the average order here takes about 47 minutes to deliver (median 44), so an RMSE of 14 to 16 minutes means the honest model's typical miss is close to a third of an average delivery.
The leaked version reports an RMSE of 862.7 seconds against 949.9 seconds for the honest version, an apparent 9% improvement that comes entirely from the leak, not from the model learning anything real.
Every model caught the store-average leak. Only three of four caught the second problem: shuffling a timestamped table instead of splitting it chronologically. Sonnet, Opus, and Fable all raised it unprompted. Fable's phrasing was the clearest: "the training set can contain orders that occurred after some of the test-set orders."
Haiku stopped after fixing the store-average feature and never mentioned the split. Opus alone added that DoorDash's own data already contains a baseline worth beating, estimated_order_place_duration plus estimated_store_to_consumer_driving_duration, and that RMSE without that comparison proves little. Readers can reproduce this one themselves.
Case 3: The Leak You Can't See in a Single Column
For this case, we are using datasets from the project Fraudsters Detection (Revolut).
The task: predict IS_FRAUDSTER for each user from their profile and transaction history.
Revolut's schema is naturally one-to-many: one users table with a label, IS_FRAUDSTER, and one transactions table with many rows per user. After joining them, one user can therefore appear across many transaction rows, all carrying the same fraud label.
The prediction problem is really about identifying fraudulent users, even though the model is trained on transaction-level rows.
Split the joined transaction rows at random, and 1,950 of 2,246 users end up with transactions on both sides of the split.
That means the test set often contains a different transaction from a user the model already saw during training.
Comparing Leakage and Non-Leakage Performance
A model trained this way scored AUC 0.960, where a higher AUC means better separation between fraudsters and non-fraudsters. Split by user instead, so nobody's transactions cross the line, and the honest AUC is 0.898: a solid model, just not the one that was reported.
All four models named the exact mechanism unprompted: the label lives at the user level, so a row-level split lets the model partly recognize a user it has already seen rather than detect fraud in a new one.
Opus's answer was the most complete, pointing out that the real sample size here is the number of users, not transactions, and that any field populated only after a fraud investigation closes is its own separate leak regardless of the split.
We ran into that ourselves while building this experiment: Revolut's account-status field is ACTIVE for every non-fraudster and LOCKED for every single fraudster in the data, a feature so leaky we removed it before touching anything else.
Case 4: The Leak That Is Not in the Data at All
For this case, we are using datasets from the project Flagging Loans (Chime).
The task: predict bad_flag, whether a loan goes bad, from columns like dpd_30_cnt, past_billings_cnt, and two credit scores.
Chime's loan data is imbalanced: about 10% of loans are flagged bad_flag = 1, with columns like dpd_30_cnt, past_billings_cnt, and two credit scores. To compensate for that imbalance, the pipeline duplicates minority-class rows before training.
We tested one short snippet, no data, no metrics attached, just code…
X_bal, y_bal = oversample(X, y) # duplicates minority rows first
X_train, X_test, y_train, y_test = train_test_split(
X_bal,
y_bal,
stratify=y_bal
)
…against the same steps reordered, split first and oversample only the training half.
Comparing Leakage and Non-Leakage Performance
Oversample-then-split reported AUC 0.986. Split-then-oversample, evaluated on a test set that was never touched, reported 0.637: a real but modest model.
The problem is that duplicating rows before the split lets identical copies of the same loan land in both halves, so the model is partly graded on examples it has effectively already seen.
All four models flagged it cold, from the snippet alone, with no accuracy number to tip them off. Opus went furthest again, noting that stratifying on already-balanced labels "is harmless but meaningless," that neither step sets a random seed, and asking whether oversampling was the right call at all versus weighting the classifier or reporting recall at a fixed review budget instead of ROC AUC.
Conclusion: What Every Model Caught
Every model we tested, including the fastest and cheapest one, caught the specific leak we planted in every case, when asked directly to review the pipeline before trusting it.
That is worth stating plainly: these four patterns are not exotic, and a model asked the right question catches them.
The difference between models showed up in what came bundled with the primary catch.
Haiku answered correctly and briefly, and in Case 2 that brevity cost it a second real leak sitting next to the one it found.
Sonnet and Fable caught both problems whenever there were two.
Opus was the only model that, across all four cases, added something beyond the leak itself: an honest accuracy expectation, a baseline worth comparing against, a leak we had not even asked about, and a challenge to whether the fix we described was the right fix.
Asking a model to review a pipeline before trusting its numbers is cheap, fast, and caught four real, planted leaks every time in this experiment. Whether it also catches the second problem sitting next to the first one depends on which model you ask, and on how much of the pipeline any reviewer, human or AI, gets to see.
Nate Rosidi is a data scientist and in product strategy. He's also an adjunct professor teaching analytics, and is the founder of StrataScratch, a platform helping data scientists prepare for their interviews with real interview questions from top companies. Nate writes on the latest trends in the career market, gives interview advice, shares data science projects, and covers everything SQL.
原文为英文,正文由自动抓取并保留原文。
原文发布于 KDnuggets(Can AI Spot Data Leakage Before Your Model Hits 99% Accuracy?),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。