Community研究与数据分析github.com

autentisitet/python-sklearn-workflow

设计无数据泄漏的 scikit-learn 拆分、交叉验证、评分、搜索、诊断曲线、PCA 缩放、模型检查、ColumnTransformer 和 Pipeline。在构建或审查 sklearn 评估工作流时使用。

python-sklearn-workflow 是什么?

该技能为开发者和 AI 代理提供了一套系统化的指南,用于在 scikit-learn 中构建无数据泄漏的评估工作流。它定义了明确的触发条件(关键词和意图),并在激活时自动提示,确保只有在涉及模型选择、交叉验证、预处理、超参数搜索或模型诊断时才应用。该技能涵盖了评估单元(独立观测值、类别标签、分组、时间序列等)的识别、将预处理(如缩放、编码、PCA)放入 Pipeline 以避免泄漏、以及有目的地选择 `cross_val_score`、`cross_validate`、`GridSearchCV` 或 `RandomizedSearchCV` 等工具。此外,它还指导何时以及如何使用学习曲线、验证曲线和 `sklearn.inspection.permutation_importance` 来回答具体的调试或可解释性问题。技能要求在每个工作流结束时输出一组结构化条目,包括独立评估单元、拆分器选择理由、搜索预算、泄漏控制措施以及最终的未触碰评估路径。该技能适用于所有支持 scikit-learn 和 Markdown 文档的工具。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/autentisitet/python-engineering-skills/tree/HEAD/skills/python-sklearn-workflow

Installed? Explore more 研究与数据分析 skills: affaan-m/uspto-database, affaan-m/scholar-evaluation, affaan-m/literature-review · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Python Scikit-Learn 工作流

触发条件

  • 关键词触发: 任务实质性涉及 scikit-learn 模型选择、KFold、StratifiedKFold、GroupKFold、TimeSeriesSplit、cross_validate、GridSearchCV、RandomizedSearchCV、学习/验证曲线、PCA、sklearn.inspection、ColumnTransformer 或 Pipeline。
  • 意图触发: 用户询问如何在无泄漏的情况下拆分、预处理、调优、诊断或解释 scikit-learn 模型。
  • 声明: 一旦开始可应用的工作,简要说明:正在使用 $python-sklearn-workflow,因为此任务涉及 scikit-learn 评估或预处理。
  • 退出: 当拆分、交叉验证、预处理、搜索和诊断合同确定后,停止应用此技能。
  • 不触发: 依赖更新、当拆分/调优已固定时的实验追踪、对已拟合估计器的普通 predict 调用、仅基于已计算预测的报告式绘图、孤立的估计器 API 问题、深度学习管道或偶然的导入不足以触发。绘图仅在任务改变评分、评估设计、诊断方法或模型检查时属于此技能范围。

从评估单元开始

在选择拆分器之前,确定在训练和评估之间必须保持独立的内容:

  • 独立的 IID 观测值;
  • 具有罕见结果的类别标签;
  • 用户、患者、设备、会话、位置或其他分组;
  • 按时间顺序排列的未来观测值;或
  • 类别平衡与不重叠分组的组合。

根据该单元选择测试集和交叉验证策略,而非根据默认百分比。阅读 references/model-selection-guide.md 了解拆分器和搜索选择。


将预处理保留在折内

  • 在拟合缩放器、填充器、编码器、特征选择器、PCA 或其他学习到的变换之前,分离出最终的测试集。
  • 将学习到的预处理和估计器放入 Pipeline 中,以便每个交叉验证折仅在其训练分区上拟合变换。
  • 当数值、类别、文本或其他列子集需要不同变换时,使用 ColumnTransformer。
  • 通过管道名称(如 preprocess__numeric__scaler 或 model__C)搜索嵌套参数。
  • 不要在 cross_val_score、cross_validate、网格搜索或随机搜索之前手动预处理整个数据集。

阅读 references/preprocessing-inspection.md 了解 PCA、检查和异构列。


有目的地选择评估和搜索工具

  • 对于单一指标和简单折分数,使用 cross_val_score。
  • 当真正需要多个指标、训练分数、拟合/评分时间或返回的估计器时,使用 cross_validate。
  • 对于参数网格较小、离散且经过论证、穷举组合适合计算预算的情况,使用 GridSearchCV。
  • 对于更大的空间、分布或固定候选预算,使用 RandomizedSearchCV。有目的地设置 n_iter 和参数分布。
  • 在模型比较之间保持相同的交叉验证语义和评分合同。当拆分器需要时,正确传递分组/元数据。
  • 保留未触碰的测试集,或当模型选择消耗了所有开发折时,使用适当的嵌套评估。

添加曲线和检查以回答问题

  • 使用 learning_curve 来询问更多训练数据是否可能减少训练/验证差距,或者两个分数是否已趋于平稳。
  • 使用 validation_curve 来检查一个超参数的训练/验证行为。它不能替代多参数搜索或最终测试。
  • 使用 sklearn.inspection.permutation_importance 来衡量拟合模型分数对特征依赖程度,最好在确认模型具有有用预测性能后,在保留数据上进行。
  • 仅当部分依赖或 ICE 的假设和特征分布使得结果可解释时,才使用它们。
  • 不要机械地添加检查图。首先定义调试、可靠性或可解释性问题。

必需输出

说明独立评估单元;保留比例的理由;选择的交叉验证拆分器和评分;预处理/PCA 放置;网格或随机搜索预算;学习/验证曲线是否回答具体问题;检查方法和数据集;泄漏控制措施;以及最终的未触碰评估路径。

Individual skills in this repo

This repo contains 3 individual skills — each has its own dedicated page.

autentisitet/python-callable-caching

Chooses Python callable ownership, including lambda versus named function and method versus module placement, plus properties, closures, cache lifetimes, construction/subclass hooks, and deterministic cleanup. Use when those design decisions affect reuse, binding, tracebacks, state, or lifetime. Do not use for ordinary calls to partial or other higher-order helpers, browser/distributed/external caches, dependency maintenance, or isolated syntax.

autentisitet/python-cli-file-workflows

Builds and reviews bounded-memory Python file-processing CLIs with explicit path bases, argparse contracts, and iterable- or callback-driven tqdm progress. Use when file scale, path ownership, progress semantics, or CLI behavior needs design. Never activate for tiny fixed fixtures that can be read wholly, swapping an already-chosen progress library, translation, documentation-only edits, dependency updates, web APIs, GUIs, shell-only scripts, or incidental Path/tqdm/argparse terms.

autentisitet/python-domain-modeling

Designs Python type annotations, domain objects, validation boundaries, dataclass/Pydantic models, invariants, interfaces, fixed choices, and object-copy semantics. Use when choosing ABC for owned nominal implementations versus Protocol for third-party structural compatibility, or when changing model behavior and boundary contracts. Never activate for dependency/version updates, translation, documentation-only edits, counting references, isolated syntax, or incidental modeling terms.

相关技能