构建防泄漏机器学习预处理流水线
将特征处理与模型训练封装为统一流水线,输出列转换方案、实现代码、边界测试及交叉验证结果。
完整提示词
<context>
Preprocessing done in pandas before the split is the most common leak in tabular ML: the scaler, imputer or target encoder learns from test rows, and cross-validation scores come out higher than production ever sees. Putting every learned step inside one Pipeline fixes this and gives a single object to save and serve. The details matter: `OneHotEncoder` needs `handle_unknown` set or a new category at inference crashes the service; rare categories should be grouped (`min_frequency`); scaling helps linear, distance and neural models but is pointless for trees; `SimpleImputer(add_indicator=True)` keeps the fact that a value was missing, which is often predictive; scikit-learn's `TargetEncoder` cross-fits internally, while a hand-rolled mean encoding leaks. A pipeline pickled with one scikit-learn version may not load with another, so the version must be pinned and recorded.
</context>
<task>
Build the preprocessing pipeline for this data: {{data}}
It feeds: {{model}}
{{#current_code}}
Existing code to replace: {{current_code}}
{{/current_code}}
1. Read the existing preprocessing and training code and the pinned scikit-learn version. Note every step that is fitted on the full dataset today.
2. Make a column plan: each column assigned to numeric, low-cardinality categorical, high-cardinality categorical, ordinal, text, datetime, identifier (dropped) or target, with the transformation and the reason. Flag columns that may leak the target.
3. Build the `ColumnTransformer` and `Pipeline`: imputation with missing indicators where useful, one-hot with unknown-category handling and rare grouping for low-cardinality, target or ordinal encoding for high-cardinality depending on the model, scaling only if the model needs it, and datetime parts derived in a stateless transformer. Use `set_output(transform="pandas")` where the version supports it so feature names survive.
4. Put the estimator as the last step, and run cross-validation on the whole pipeline so every fold refits preprocessing.
5. Save the fitted pipeline as one artifact with the scikit-learn version and the expected input columns and types recorded next to it.
6. Write tests: unseen category, all-missing column value, missing column (fails with a clear message), extra column (ignored or rejected, as decided), wrong dtype, a single-row input, and a reload of the saved artifact giving identical predictions.
7. Run the tests and a cross-validation, and compare the score with the old approach if one existed; a drop usually means the old score was leaking.
</task>
<constraints>
- No learned statistic may be computed outside the pipeline.
- Keep column lists in one place; do not duplicate them between training and serving code.
- Use only transformers available in the pinned scikit-learn version; if a feature needs a newer version, say so instead of upgrading silently.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Column plan
Table: column | group | transformation | reason.
## Code
Code blocks with file paths.
## Tests
Code blocks with file paths.
## Check
Commands run, cross-validation result old versus new, and real test results.
## Open questions
Numbered, or "None".
</output_format>可替换内容
- {{data}}
- {{model}}
- {{#current_code}}
- {{current_code}}
- {{/current_code}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、机器学习、特征预处理、数据泄漏防范、交叉验证、模型持久化