executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
ds-ensemble-methods"Provides Combines multiple models using bagging, boosting, stacking voting, and blending for improved predictive performance and robustness"
Independent GitHub snapshot · Automated classification · Original documentation governs
name: ds-ensemble-methods compatibility: opencode completeness: 95 content-types:
Comprehensive guide to ensemble methods in machine learning and data science workflows.
Ensemble Methods is a critical component of the machine learning workflow. This skill covers:
# BAD: Hardcoded hyperparameters, no validation, data leakage risk
# from sklearn.ensemble import RandomForestClassifier
# rf = RandomForestClassifier(n_estimators=100)
# rf.fit(X, y)
# print(rf.score(X, y)) # Evaluating on training data violates KISS principle
# GOOD: Follows DRY/SOLID principles, proper train/test split, cross-validation, type hints
import pandas as pd
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.ensemble import VotingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from typing import Dict, Any, Tuple
def train_basic_ensemble(X: np.ndarray, y: np.ndarray) -> Dict[str, Any]:
"""Train a voting ensemble with proper validation."""
if X.shape[0] != y.shape[0]:
raise ValueError("Feature and target dimensions must match")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
base_estimators = [
('logreg', LogisticRegression(random_state=42, max_iter=1000))
('dt', DecisionTreeClassifier(random_state=42))
]
ensemble = VotingClassifier(estimators=base_estimators, voting='soft')
cv_scores = cross_val_score(ensemble, X_train, y_train, cv=5, scoring='accuracy')
ensemble.fit(X_train, y_train)
return {
'test_accuracy': accuracy_score(y_test, ensemble.predict(X_test))
'cv_mean': float(cv_scores.mean())
'cv_std': float(cv_scores.std())
}
# Usage
X, y = make_classification(n_samples=800, n_features=8, n_classes=2, random_state=42)
results = train_basic_ensemble(X, y)
print(f"Test Accuracy: {results['test_accuracy']:.4f} | CV: {results['cv_mean']:.4f} ± {results['cv_std']:.4f}")
import logging
import pandas as pd
import numpy as np
from typing import Any, Dict, List
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, f1_score
from sklearn.base import BaseEstimator, ClassifierMixin
logger = logging.getLogger(__name__)
class EnsembleMethods:
"""Production implementation of Ensemble Methods following SOLID principles."""
def __init__(self, n_estimators: int = 100, random_state: int = 42) -> None:
self.n_estimators = n_estimators
self.random_state = random_state
self.models: Dict[str, Any] = {}
self.metrics: Dict[str, float] = {}
def execute(self, data: pd.DataFrame, target_col: str) -> Dict[str, Any]:
"""Execute Ensemble Methods on data with full validation."""
if target_col not in data.columns:
raise ValueError(f"Target column '{target_col}' not found in data")
X = data.drop(columns=[target_col])
y = data[target_col]
if X.isnull().any().any():
raise ValueError("Input data contains missing values")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=self.random_state, stratify=y
)
base_ests = [
('rf', RandomForestClassifier(n_estimators=self.n_estimators, random_state=self.random_state))
('gb', GradientBoostingClassifier(n_estimators=self.n_estimators, random_state=self.random_state))
]
meta_ests = LogisticRegression(random_state=self.random_state, max_iter=1000)
stack_clf = StackingClassifier(estimators=base_ests, final_estimator=meta_ests, cv=5)
stack_clf.fit(X_train, y_train)
y_pred = stack_clf.predict(X_test)
self.metrics = {
'accuracy': float(accuracy_score(y_test, y_pred))
'f1_score': float(f1_score(y_test, y_pred, average='weighted'))
}
logger.info(f"Ensemble training complete. Accuracy: {self.metrics['accuracy']:.4f}")
return {
'status': 'success'
'metrics': self.metrics
'predictions': y_pred.tolist()
'model_type': 'StackingClassifier'
}
| Pitfall | Problem | Solution | |
Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.