Sub topic 1
What Feature Engineering Is and Why It Matters
Prerequisites
This chapter assumes you know:
- Basic Python and PySpark DataFrame operations.
- Delta tables and Databricks notebooks.
- The basic machine learning workflow: training, evaluation, and inference.
- Unity Catalog's
catalog.schema.tablenaming.
What a feature is
A feature is an input value a model uses to make a prediction. Raw data rarely arrives in that form. An orders table holds one row per purchase, but a churn model needs one row per user. Feature engineering is the work of turning raw records into those model-ready values.
Common transformations include:
- Aggregating events, such as the number of orders in the last 90 days.
- Encoding categories as numbers.
- Scaling numeric columns.
- Filling in missing values.
A worked example
The following PySpark code reduces a raw orders table to one row of features per user.
from pyspark.sql import functions as F
orders = spark.table("main.sales.orders")
user_features_df = (
orders
.filter(F.col("order_ts") >= F.date_sub(F.current_date(), 90))
.groupBy("user_id")
.agg(
F.count("*").alias("order_count_90d"),
F.avg("amount").alias("avg_order_amount_90d"),
F.max("order_ts").alias("last_order_ts"),
)
)The result has one row per user_id. The column names include the window (90d), so a reader knows what each value means. The filter limits the data to recent behavior, which is often more predictive than a lifetime total.
Why it matters
Model quality depends heavily on feature quality. A simple model with well-designed features often beats a complex model fed raw columns.
Features also create operational risk. The model sees features at two points in its life:
- At training time, when you compute them from historical data.
- At inference time, when you compute them for new data.
If the two computations differ, the model receives inputs it was never trained on. This mismatch is called training-serving skew. It often happens when a data scientist writes feature code in a notebook and an engineer later rewrites it for production.
The rest of this tutorial addresses that risk. You will compute each feature once, store it in a governed Unity Catalog feature table, and reuse it for training and scoring.
Common errors
- Computing a feature differently in training and serving code.
- Using a window that includes data after the prediction time. This leaks information from the future and inflates evaluation scores.
- Leaving missing values unhandled, so they behave differently across environments.
- Giving columns vague names such as
value1, which makes reuse by other teams difficult.
Exercise
Your churn model scores well in testing but performs poorly in production. The training notebook computes order_count_90d with Spark, and the production service uses its own reimplementation. What is the most likely cause, and what is the general fix?
Show answer
The likely cause is training-serving skew: the two implementations compute the feature differently. The fix is to compute the feature once, store it in a shared feature table, and have both training and inference read from it.