machine-learning
Supervised & unsupervised learning, scikit-learn, XGBoost, model evaluation, feature engineering for production ML
What this skill does
# Machine Learning
Production-grade machine learning with scikit-learn, XGBoost, and modern ML engineering practices.
## Quick Start
```python
# Production ML Pipeline with scikit-learn
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, cross_val_score
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 RandomForestClassifier
from sklearn.metrics import classification_report, roc_auc_score
import joblib
# Load and split data
df = pd.read_csv("data/customers.csv")
X = df.drop("churn", axis=1)
y = df["churn"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Define feature types
numeric_features = ["age", "tenure", "monthly_charges"]
categorical_features = ["contract_type", "payment_method"]
# Build preprocessing pipeline
numeric_transformer = Pipeline([
("imputer", SimpleImputer(strategy="median")),
("scaler", StandardScaler())
])
categorical_transformer = Pipeline([
("imputer", SimpleImputer(strategy="constant", fill_value="missing")),
("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", RandomForestClassifier(n_estimators=100, random_state=42))
])
# Train and evaluate
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]
print(classification_report(y_test, y_pred))
print(f"ROC-AUC: {roc_auc_score(y_test, y_prob):.4f}")
# Save model
joblib.dump(model, "models/churn_model.joblib")
```
## Core Concepts
### 1. Feature Engineering Pipeline
```python
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.preprocessing import FunctionTransformer
import numpy as np
class DateFeatureExtractor(BaseEstimator, TransformerMixin):
"""Custom transformer for date features."""
def __init__(self, date_column: str):
self.date_column = date_column
def fit(self, X, y=None):
return self
def transform(self, X):
X = X.copy()
dates = pd.to_datetime(X[self.date_column])
X["day_of_week"] = dates.dt.dayofweek
X["month"] = dates.dt.month
X["is_weekend"] = (dates.dt.dayofweek >= 5).astype(int)
X["days_since_epoch"] = (dates - pd.Timestamp("1970-01-01")).dt.days
return X.drop(self.date_column, axis=1)
class OutlierClipper(BaseEstimator, TransformerMixin):
"""Clip outliers to percentile bounds."""
def __init__(self, lower_percentile=1, upper_percentile=99):
self.lower_percentile = lower_percentile
self.upper_percentile = upper_percentile
self.bounds_ = {}
def fit(self, X, y=None):
for col in X.columns:
self.bounds_[col] = (
np.percentile(X[col], self.lower_percentile),
np.percentile(X[col], self.upper_percentile)
)
return self
def transform(self, X):
X = X.copy()
for col, (lower, upper) in self.bounds_.items():
X[col] = X[col].clip(lower, upper)
return X
# Log transform for skewed features
log_transformer = FunctionTransformer(
func=lambda x: np.log1p(np.maximum(x, 0)),
inverse_func=lambda x: np.expm1(x)
)
```
### 2. Cross-Validation Strategies
```python
from sklearn.model_selection import (
StratifiedKFold, TimeSeriesSplit, GroupKFold,
cross_val_score, cross_validate
)
# Stratified K-Fold (for imbalanced classification)
stratified_cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
scores = cross_val_score(
model, X, y,
cv=stratified_cv,
scoring="roc_auc",
n_jobs=-1
)
print(f"ROC-AUC: {scores.mean():.4f} (+/- {scores.std()*2:.4f})")
# Time Series Split (for temporal data)
ts_cv = TimeSeriesSplit(n_splits=5, gap=7) # 7-day gap
for train_idx, test_idx in ts_cv.split(X):
X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]
# Train and evaluate...
# Group K-Fold (prevent data leakage by user/entity)
group_cv = GroupKFold(n_splits=5)
groups = df["user_id"] # Same user never in train and test
scores = cross_val_score(
model, X, y,
cv=group_cv,
groups=groups,
scoring="roc_auc"
)
# Multiple metrics at once
results = cross_validate(
model, X, y,
cv=stratified_cv,
scoring=["accuracy", "precision", "recall", "f1", "roc_auc"],
return_train_score=True
)
```
### 3. Hyperparameter Tuning
```python
from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import randint, uniform
import optuna
# RandomizedSearchCV (good baseline)
param_dist = {
"classifier__n_estimators": randint(100, 500),
"classifier__max_depth": randint(3, 15),
"classifier__min_samples_split": randint(2, 20),
"classifier__min_samples_leaf": randint(1, 10),
}
random_search = RandomizedSearchCV(
model,
param_distributions=param_dist,
n_iter=50,
cv=stratified_cv,
scoring="roc_auc",
n_jobs=-1,
random_state=42,
verbose=1
)
random_search.fit(X_train, y_train)
print(f"Best params: {random_search.best_params_}")
print(f"Best score: {random_search.best_score_:.4f}")
# Optuna (modern, efficient)
def objective(trial):
params = {
"n_estimators": trial.suggest_int("n_estimators", 100, 500),
"max_depth": trial.suggest_int("max_depth", 3, 15),
"min_samples_split": trial.suggest_int("min_samples_split", 2, 20),
"learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3, log=True),
}
model = XGBClassifier(**params, random_state=42)
scores = cross_val_score(model, X_train, y_train, cv=5, scoring="roc_auc")
return scores.mean()
study = optuna.create_study(direction="maximize")
study.optimize(objective, n_trials=100, n_jobs=-1)
print(f"Best params: {study.best_params}")
```
### 4. XGBoost Production Pattern
```python
import xgboost as xgb
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
# Prepare DMatrix for efficiency
dtrain = xgb.DMatrix(X_train, label=y_train, enable_categorical=True)
dtest = xgb.DMatrix(X_test, label=y_test, enable_categorical=True)
params = {
"objective": "binary:logistic",
"eval_metric": ["logloss", "auc"],
"max_depth": 6,
"learning_rate": 0.1,
"subsample": 0.8,
"colsample_bytree": 0.8,
"min_child_weight": 1,
"tree_method": "hist", # Fast histogram-based
"device": "cuda", # GPU if available
"random_state": 42,
}
# Train with early stopping
evals = [(dtrain, "train"), (dtest, "eval")]
model = xgb.train(
params,
dtrain,
num_boost_round=1000,
evals=evals,
early_stopping_rounds=50,
verbose_eval=100
)
# Feature importance
importance = model.get_score(importance_type="gain")
sorted_importance = dict(sorted(importance.items(), key=lambda x: x[1], reverse=True))
# SHAP values for interpretability
import shap
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)
shap.summary_plot(shap_values, X_test, plot_type="bar")
```
### 5. Handling Imbalanced Data
```python
from imblearn.over_sampling import SMOTE, ADASYN
from imblearn.under_sampling import RandomUnderSampler
from imblearn.pipeline import Pipeline as ImbPipeline
from sklearn.utils.class_weight import compute_class_weight
# Option 1: Class weights
class_weights = compute_class_weight("balanced", classes=np.unique(y_train), y=y_train)
weight_dict = dict(zip(np.unique(y_train), class_weights))
model = RandomForestClassifier(class_weight=weight_dict)
# Option 2: SMOTE oversampling
smote = SMOTE(random_state=42, sampling_stRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.