Community研究與資料分析github.com

autentisitet/python-sklearn-workflow

設計無資料洩漏的 scikit-learn 分割、交叉驗證、評分、搜尋、診斷曲線、PCA 縮放、模型檢驗、ColumnTransformer 與 Pipeline。用於建構或審查 sklearn 評估工作流程時。

python-sklearn-workflow 是什麼?

此技能專門用於建構安全、無資料洩漏的 scikit-learn 評估工作流程。它引導使用者從定義評估單元(如獨立觀測值、類別標籤、群組或時間序列)開始,然後選擇合適的測試集與交叉驗證(CV)策略,如 KFold、StratifiedKFold、GroupKFold 或 TimeSeriesSplit。技能強調預處理(如標準化、PCA)必須置於 Pipeline 內部,並使用 ColumnTransformer 處理異質欄位,以確保每次 CV 摺疊都獨立擬合。進階功能包括使用學習曲線與驗證曲線診斷模型行為,以及透過 permutation importance 等工具進行模型檢驗。此技能適用於 Claude Code、Cursor、Codex 等 AI 代理平台,開發者可在模型選擇、超參數搜尋(GridSearchCV 或 RandomizedSearchCV)或評估設計變更時調用,但在已擬合模型進行預測或實驗追蹤已固定時不應使用。

相容平台~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 評估或預處理。
  • 退出: 當分割、CV、預處理、搜尋與診斷合約確定後,停止套用此技能。
  • 請勿觸發: 相依性更新、分割/調參已固定時的實驗追蹤、對已擬合模型的普通 predict 呼叫、從已計算預測結果僅輸出報表、隔離的估計器 API 問題、深度學習流程或偶然的導入皆不足以觸發。只有在任務改變評分、評估設計、診斷方法或模型檢驗時,繪圖才屬於此處範圍。

從評估單元開始

在選擇分割器之前,先識別哪些部分必須在訓練與評估之間保持獨立:

  • 個別 IID 觀測值;
  • 具有稀有結果的類別標籤;
  • 使用者、患者、裝置、會話、位置或其他群組;
  • 時間序列的未來觀測值;或
  • 類別平衡與非重疊群組的組合。

根據該單元選擇測試集與 CV 策略,而不是根據偏好的預設百分比。請參閱 references/model-selection-guide.md 以了解分割器與搜尋選擇。


將預處理保留在摺疊內部

  • 在擬合縮放器、插補器、編碼器、特徵選擇器、PCA 或其他學習轉換之前,先分割出最終測試集。
  • 將學習到的預處理與估計器放入 Pipeline,讓每個 CV 摺疊僅在其訓練分區上擬合轉換。
  • 當數值、類別、文字或其他欄位子集需要不同轉換時,使用 ColumnTransformer。
  • 透過 Pipeline 名稱(如 preprocess__numeric__scaler 或 model__C)搜尋巢狀參數。
  • 請勿在 cross_val_score、cross_validate、網格搜尋或隨機搜尋之前手動預處理整個資料集。

請參閱 references/preprocessing-inspection.md 以了解 PCA、檢驗與異質欄位。


審慎選擇評估與搜尋工具

  • 使用 cross_val_score 處理單一指標與簡單摺疊分數。
  • 當確實需要多個指標、訓練分數、擬合/評分時間或回傳估計器時,使用 cross_validate。
  • 對於小型、離散、有正當理由的參數網格,且窮舉組合符合計算預算時,使用 GridSearchCV。
  • 對於較大空間、分佈或固定候選預算時,使用 RandomizedSearchCV。審慎設定 n_iter 與參數分佈。
  • 在模型比較之間保持相同的 CV 語意與評分合約。當分割器需要時,正確傳遞群組/中繼資料。
  • 保留未觸及的測試集,或在模型選擇消耗所有開發摺疊時使用適當的巢狀評估。

加入曲線與檢驗以回答問題

  • 使用 learning_curve 提問更多訓練資料是否可能減少訓練/驗證差距,或兩者分數是否已趨於平緩。
  • 使用 validation_curve 檢查單一超參數的訓練/驗證行為。它不能取代多參數搜尋或最終測試。
  • 使用 sklearn.inspection.permutation_importance 測量已擬合模型分數對特徵的依賴性,最好在確認模型具有實用預測效能後,於保留資料上進行。
  • 僅當部分相依性或 ICE 的假設與特徵分佈使結果可解釋時,才使用它們。
  • 請勿機械性地加入檢驗圖表。先定義除錯、可靠性或可解釋性的問題。

必要輸出

陳述獨立評估單元、保留比例的理由、選擇的 CV 分割器與評分方法、預處理/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.

相關技能