Python机器学习

Mattakushi4322026-10-26查看 1077复制 803

何时激活:scikit-learn、机器学习管道、特征工程、模型评估、交叉验证

Download (.md)

📘如何使用这个技能

  1. 1
    复制技能内容·点击上方「Copy SKILL.md」复制全部内容,或 Download 下载到本地
  2. 2
    打开你的 AI 客户端·支持 Claude Code、Trae、Cursor、ChatGPT、DeepSeek、Kimi 等任意 AI 助手
  3. 3
    粘贴为系统提示/知识文件·Claude Code:放入 .claude/skills/ 目录;普通对话:作为首条消息发送并说「请严格按这份技能文档执行」
  4. 4
    直接提出你的需求·AI 会按技能定义的工作流、格式和约束来处理,无需再解释背景

💡 提示:技能生效后,每次对话只需一句需求,AI 会按 SKILL.md 的流程执行。

SKILL.md3586 chars
---
name: python-ml
description: "When to activate: scikit-learn, machine learning pipelines, feature engineering, model evaluation, cross-validation"
origin: zakr
metadata:
  namespace: zakr/devtools-pack
  version: 1.0.0
  category: ml
  language: python
  platforms: [claude-code, codex, gemini-cli, cursor, aider, windsurf]
---

# Python ML Patterns (scikit-learn)

## Pipeline Pattern
```python
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score, StratifiedKFold

# Feature groups
numeric_features = ["age", "income", "tenure"]
categorical_features = ["gender", "country", "plan"]

# Preprocessing
numeric_transformer = Pipeline([
    ("imputer", SimpleImputer(strategy="median")),
    ("scaler", StandardScaler()),
])

categorical_transformer = Pipeline([
    ("imputer", SimpleImputer(strategy="most_frequent")),
    ("encoder", OneHotEncoder(handle_unknown="ignore", sparse_output=False)),
])

preprocessor = ColumnTransformer([
    ("num", numeric_transformer, numeric_features),
    ("cat", categorical_transformer, categorical_features),
])

# Full pipeline
model = Pipeline([
    ("preprocessor", preprocessor),
    ("classifier", GradientBoostingClassifier(n_estimators=100, random_state=42)),
])
```

## Evaluation
```python
import numpy as np
from sklearn.metrics import classification_report, roc_auc_score

# Cross-validation (never evaluate on training data)
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(model, X_train, y_train, cv=cv, scoring="roc_auc")
print(f"AUC: {scores.mean():.3f} ± {scores.std():.3f}")

# After final model training on full train set
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_proba = model.predict_proba(X_test)[:, 1]

print(classification_report(y_test, y_pred))
print(f"Test AUC: {roc_auc_score(y_test, y_proba):.3f}")
```

## Hyperparameter Tuning
```python
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint, uniform

param_distributions = {
    "classifier__n_estimators": randint(50, 300),
    "classifier__max_depth": randint(3, 10),
    "classifier__learning_rate": uniform(0.01, 0.3),
    "classifier__subsample": uniform(0.6, 0.4),
}

search = RandomizedSearchCV(
    model,
    param_distributions,
    n_iter=50,
    cv=5,
    scoring="roc_auc",
    n_jobs=-1,
    random_state=42,
)
search.fit(X_train, y_train)
print(f"Best params: {search.best_params_}")
print(f"Best AUC: {search.best_score_:.3f}")
```

## Model Persistence
```python
import joblib
from pathlib import Path

model_path = Path("models/churn_model_v1.joblib")

# Save
joblib.dump(model, model_path)

# Load
loaded_model = joblib.load(model_path)

# Always save with metadata
import json
metadata = {
    "model_version": "1.0.0",
    "trained_at": datetime.utcnow().isoformat(),
    "features": numeric_features + categorical_features,
    "target": "churned",
    "metrics": {"test_auc": 0.892},
}
(model_path.parent / "metadata.json").write_text(json.dumps(metadata, indent=2))
```

## Anti-Patterns
- Data leakage: fitting preprocessor on full dataset before split
- Not using Pipeline (manual preprocessing on test set introduces bugs)
- Evaluating on training data
- Using accuracy for imbalanced classes (use AUC or F1)
- Missing random_state for reproducibility
- Storing large datasets in git (use DVC or S3)
来源:skillsdirectory

评论0

登录后参与评论

还没有评论,来抢沙发吧