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-feature-engineering"Creates and transforms features including polynomial features, interactions domain-specific features, and feature transformations"
Independent GitHub snapshot · Automated classification · Original documentation governs
name: ds-feature-engineering compatibility: opencode completeness: 95 content-types:
Comprehensive guide to feature engineering in machine learning and data science workflows.
Feature Engineering is a critical component of the machine learning workflow. This skill covers:
import pandas as pd
import numpy as np
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
def basic_feature_engineering(df: pd.DataFrame, target_col: str, numeric_cols: list) -> tuple:
"""
Apply basic feature engineering transformations to a DataFrame.
Handles polynomial features, log transforms, and standard scaling.
"""
if df.empty:
raise ValueError("Input DataFrame cannot be empty")
# Separate features and target
X = df.drop(columns=[target_col])
y = df[target_col]
# Identify numeric columns for transformation
numeric_features = [col for col in numeric_cols if col in X.columns]
# Create transformation pipeline
transformer = ColumnTransformer(
transformers=[
('poly', PolynomialFeatures(degree=2, include_bias=False), numeric_features)
('scaler', StandardScaler(), numeric_features)
]
remainder='passthrough'
)
# Fit and transform data
X_transformed = transformer.fit_transform(X)
# Create DataFrame with new feature names
poly_names = transformer.named_transformers_['poly'].get_feature_names_out(numeric_features)
new_df = pd.DataFrame(X_transformed, columns=poly_names, index=X.index)
return new_df, transformer, y
import logging
import pandas as pd
import numpy as np
from typing import Any, Dict, List, Optional
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
import warnings
logger = logging.getLogger(__name__)
warnings.filterwarnings('ignore')
class FeatureEngineering:
"""Production-grade feature engineering pipeline with validation and logging."""
def __init__(self, numeric_cols: List[str], categorical_cols: List[str],
target_col: str, degree: int = 2, handle_missing: bool = True):
self.numeric_cols = numeric_cols
self.categorical_cols = categorical_cols
self.target_col = target_col
self.degree = degree
self.handle_missing = handle_missing
self.pipeline = None
self.feature_names = None
def _validate_input(self, data: pd.DataFrame) -> None:
missing_cols = set(self.numeric_cols + self.categorical_cols + [self.target_col]) - set(data.columns)
if missing_cols:
raise ValueError(f"Missing required columns: {missing_cols}")
if data.isnull().sum().sum() > 0 and not self.handle_missing:
logger.warning("Data contains missing values but handle_missing is False")
def execute(self, data: pd.DataFrame) -> Dict[str, Any]:
"""Execute feature engineering on data with full validation and logging."""
self._validate_input(data)
logger.info(f"Starting feature engineering on {len(data)} rows")
X = data.drop(columns=[self.target_col])
y = data[self.target_col]
# Build preprocessing pipeline
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')) if self.handle_missing else None
('scaler', StandardScaler())
])
categorical_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')) if self.handle_missing else None
('encoder', OneHotEncoder(handle_unknown='ignore', sparse_output=False))
])
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, self.numeric_cols)
('cat', categorical_transformer, self.categorical_cols)
]
remainder='drop'
)
# Fit and transform
X_transformed = preprocessor.fit_transform(X)
self.pipeline = preprocessor
# Get feature names
num_names = preprocessor.named_transformers_['num'].named_steps['scaler'].get_feature_names_out(self.numeric_cols)
cat_names = preprocessor.named_transformers_['cat'].named_steps['encoder'].get_feature_names_out(self.categorical_cols)
self.feature_names = np.concatenate([num_names, cat_names])
logger.info(f"Feature engineering complete. Output shape: {X_transformed.shape}")
return {
'X_transformed': X_transformed
'y': y.values
'feature_names': self.feature_names
'pipeline': self.pipeline
'metadata': {'original_shape': data.shape, 'transformed_shape': X_transformed.shape}
}
| 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.