liveBook · Manning

Feature engineering

As explained in Digital Twins in Action

Feature engineering is the process of constructing informative input variables from raw data for analysis or machine learning.

Feature engineering constructs input variables that expose patterns or context missing from raw measurements. A sensor reading may report 185°C; deviation from an optimal temperature, rate of change, and time above a safe range give that reading operational meaning. Unlike an embedding, which encodes similarity in a dense vector, an engineered feature can describe a specific, interpretable relationship. section 4.1.5

Start with clean, normalized data synchronized to a common time grid. Then choose transformations that reflect how the physical system behaves and what the prediction needs to capture. section 8.1.3

For a home digital twin, three useful feature types are:

  • Temporal features: encode hour of day as sine and cosine values so 23:00 and 00:00 remain close, rather than appearing at opposite ends of a 0–23 scale. section 8.1.3
  • Lagged features: shift earlier indoor temperatures into the current row to expose the previous three hours of temperature change and the building’s thermal memory. section 8.1.3
  • Domain-specific features: subtract outdoor temperature from indoor temperature to represent a temperature difference that neither reading expresses alone. section 8.1.3
Listing 8.2 Cyclical encoding of time
import numpy as np
import pandas as pd

df = pd.DataFrame({"hour": range(24)})

df["hour_sin"] = np.sin(2 * np.pi * df["hour"] / 24)
df["hour_cos"] = np.cos(2 * np.pi * df["hour"] / 24)

The NumPy and pandas listing creates two columns for hour of day. Together, the sine and cosine values represent time on a cycle. section 8.1.3

Listing 8.3 Calculating short-term temperature lag features
import pandas as pd

data = {"indoor_temp": [20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5,
24.0, 24.5]}
df = pd.DataFrame(data)

df["indoor_temp_lag_1"] = df["indoor_temp"].shift(1)
df["indoor_temp_lag_2"] = df["indoor_temp"].shift(2)
df["indoor_temp_lag_3"] = df["indoor_temp"].shift(3)

df = df.dropna()

print(
    df[["indoor_temp", "indoor_temp_lag_1", "indoor_temp_lag_2",
    "indoor_temp_lag_3"]]
)

The pandas listing uses shift(1), shift(2), and shift(3) to add prior temperatures, then drops rows without a complete three-hour history. section 8.1.3

Temperature difference as a feature

At midnight, an indoor reading of 22.1°C and an outdoor reading of 8.2°C produce a 13.9°C temperature delta. Calculate the same difference for each synchronized row to give a model an explicit measure of the indoor–outdoor relationship. section 8.1.3

Feature stores such as Hopsworks and Feast can centralize engineered variables, maintain training–serving consistency, and provide versioning, lineage, and low-latency access. AutoML libraries such as FLAML, AutoKeras, and AutoGluon can automate feature engineering alongside model selection and parameter tuning; they offer an alternative to explicitly constructing every feature by hand. section 4.3.6 section 8.3.2

How Digital Twins in Action covers it

How deep it goes
Works through cyclical, lagged, and domain-specific transformations with pandas examples, while deferring some feature construction to AutoML.
Examples
Uses temperature deviations, cyclically encoded hours, three-hour temperature lags, and an indoor–outdoor temperature difference.
What it assumes
Assumes sensor data is clean, normalized, and synchronized before feature extraction; explains the three transformation types through a home example.
What you can do after
Build it from scratch with NumPy and pandas transformations, or apply it via AutoML libraries.
How it connects
Connects to derived data, time-series forecasting, feature stores, and automated machine learning.

Also explained in

Compare how 16 books explain feature engineering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.