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-model-interpretation"Provides Interprets models using SHAP values, LIME, feature importance permutation importance, and other explainability techniques"
Independent GitHub snapshot · Automated classification · Original documentation governs
name: ds-model-interpretation compatibility: opencode completeness: 95 content-types:
Comprehensive guide to model interpretation in machine learning and data science workflows.
Model Interpretation is a critical component of the machine learning workflow. This skill covers:
import pandas as pd
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
def basic_model_interpretation():
# Load dataset and split into train/test sets
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.2, random_state=42
)
feature_names = data.feature_names
# Train a tree-based model for intrinsic feature importance
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Extract and sort feature importances
importances = model.feature_importances_
indices = np.argsort(importances)[::-1]
# Visualize results
plt.figure(figsize=(10, 6))
plt.title("Feature Importances")
plt.bar(range(X_train.shape[1]), importances[indices], align="center")
plt.xticks(range(X_train.shape[1]), [feature_names[i] for i in indices], rotation=90)
plt.tight_layout()
plt.show()
return {
"feature_importances": dict(zip(feature_names, importances))
"top_features": [feature_names[i] for i in indices[:5]]
}
if __name__ == "__main__":
results = basic_model_interpretation()
print("Top 5 features:", results["top_features"])
import logging
import pandas as pd
import numpy as np
from typing import Any, Dict, List
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.inspection import permutation_importance
from sklearn.datasets import load_wine
logger = logging.getLogger(__name__)
class ModelInterpretation:
"""Production implementation of Model Interpretation"""
def __init__(self, n_repeats: int = 10, random_state: int = 42):
self.n_repeats = n_repeats
self.random_state = random_state
self.model = None
self.feature_names: List[str] = []
def execute(self, data: pd.DataFrame, target_col: str) -> Dict[str, Any]:
"""Execute Model Interpretation on data"""
try:
if data is None or data.empty:
raise ValueError("Input data cannot be None or empty")
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]
self.feature_names = list(X.columns)
self.model = GradientBoostingClassifier(n_estimators=50, random_state=self.random_state)
self.model.fit(X, y)
result = permutation_importance(
self.model, X, y, n_repeats=self.n_repeats,
random_state=self.random_state, scoring="accuracy"
)
importance_df = pd.DataFrame({
"feature": self.feature_names
"importance_mean": result.importances_mean
"importance_std": result.importances_std
}).sort_values(by="importance_mean", ascending=False)
logger.info(f"Interpretation complete. Top feature: {importance_df.iloc[0]['feature']}")
return {"importance_results": importance_df.to_dict(orient="records")}
except Exception as e:
logger.error(f"Interpretation failed: {str(e)}")
raise RuntimeError(f"Model interpretation execution failed: {e}") from e
if __name__ == "__main__":
wine = load_wine()
df = pd.DataFrame(wine.data, columns=wine.feature_names)
df["target"] = wine.target
interpreter = ModelInterpretation()
results = interpreter.execute(df, "target")
print("Production interpretation results:", results["importance_results"][:3])
| 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.