Full Feature Engineering Pipeline

This example demonstrates a comprehensive feature engineering pipeline combining multiple transformer categories: bivector interactions, whitening, phase encoding, and density esti

Open In Colab

# %%
# Setup
# -----
# Import libraries and suppress warnings.

import warnings
warnings.filterwarnings("ignore")

import numpy as np
from modeva import DataSet

# %%
# Load Dataset
# ------------

ds = DataSet()
ds.load(name="CaliforniaHousing")
ds.set_random_split()

# Keep a copy of the raw (un-engineered) data; the fitted pipeline is applied
# to raw-feature input below, not to the already-engineered frame.
raw_df = ds.to_df()

original_n_features = len(ds.feature_names)
print(f"Original features ({original_n_features}): {ds.feature_names}")

# %%
# Build Pipeline
# --------------
# Queue multiple feature engineering steps across different categories.

# Pairwise interactions
ds.fe_bivector(max_features=30, selection="variance")

# Coordinate transform
ds.fe_whitening()

# Pairwise shear
ds.fe_shear(threshold=0.01)

# Phase encoding
ds.fe_phase_encoder(n_thresholds=3, include_original=False)

# Local density
ds.fe_density(bandwidth="median", n_reference=300)

# %%
# Execute Pipeline
# ----------------

ds.engineer_features()

new_n_features = len(ds.all_feature_names)
print(f"Features: {original_n_features} -> {new_n_features}")
print(f"New features added: {new_n_features - original_n_features}")

# %%
# Verify Transform on New Data
# ----------------------------
# Apply the fitted pipeline to a fresh batch of data.

test_df = raw_df.iloc[:3]
transformed = ds.transform_features(test_df)

print(f"Input shape:  {test_df.shape}")
print(f"Output shape: {transformed.shape}")
print(f"All columns match: {list(transformed.columns) == list(ds.to_df().columns)}")