Sub topic 1
What Feature Engineering Is and Why It Matters
Prerequisites
Assume basic Python, PySpark DataFrame operations, Delta tables, Databricks notebooks, and the catalog.schema.table naming pattern in Unity Catalog.
What Feature Engineering Is
Feature engineering converts raw records into useful model inputs. A feature is a measurable value, such as a user's order count or average order amount. Good features represent information that was available when a prediction was made.
Features should be reusable. Computing the same feature separately for every model can create inconsistent definitions and extra maintenance.
Computing Features from Raw Tables with Spark
Spark processes large tables across a cluster. Use DataFrame transformations to filter records, group them by an entity, and calculate aggregates.
from pyspark.sql import functions as F
orders = spark.table("main.sales.orders")
user_features = (
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. Each column describes recent behavior.
Encoding Categories, Scaling, and Missing Values
Models usually need numeric inputs. Encode categories such as "gold" and "silver" as numbers or one-hot columns. Scaling puts numeric columns on comparable ranges when the algorithm needs it. Missing-value handling replaces nulls with a justified default, a statistic such as the median, or an explicit “unknown” category.
Apply the same transformations during training and inference. Store their definitions with the feature pipeline when possible.
Why Training and Serving Features Drift Apart
Training-serving skew occurs when training and production use different feature logic. For example, training might calculate a 90-day order count, while serving accidentally uses 30 days. Time zones, null handling, and category mappings can also differ.
Central feature definitions reduce this risk.
Unity Catalog Essentials for Feature Storage
Unity Catalog organizes data as catalog.schema.table and controls access through permissions. It also supports discovery, auditing, and lineage. Grant users only the permissions they need to read or update feature data.
What a Feature Table Is
A feature table is a governed Delta table containing reusable features and a key that identifies each entity. A primary key such as user_id identifies the row. Time-dependent tables can also include a timestamp column so lookups use the feature value known at that time.
Exercise
Why should a feature table have a primary key?
Show answer
The key identifies which entity each feature row describes and lets lookup operations join features to labels or prediction inputs.