# Python Ml

> When to activate: scikit-learn, machine learning pipelines, feature engineering, model evaluation, cross-validation

- Skill: `mattakushi432/python-ml` (Agent Skill)
- Install (CLI): `npx skillmds@latest add mattakushi432/python-ml`
- Raw SKILL.md: https://api.skillmd.com/api/skills/mattakushi432/python-ml/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: Mattakushi432 (https://skillmd.com/u/mattakushi432)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/mattakushi432/python-ml

---


# 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)

