Sub topic 3
Point-in-Time Correctness for Time-Dependent Features
Assume basic Python, PySpark, Delta tables, Unity Catalog, and model training. This chapter focuses on preventing time leakage and operating reusable features in production.
Point-in-time correctness
A point-in-time lookup uses only feature values that existed when the labeled event occurred. Without it, a training row for January might accidentally use a customer value computed in February. The model then sees information unavailable during real prediction.
Declare the feature timestamp and use the label event timestamp as the lookup time:
from databricks.feature_engineering import FeatureEngineeringClient, FeatureLookup
fe = FeatureEngineeringClient()
fe.create_table(
name="main.ml.user_activity_features",
primary_keys=["user_id", "feature_ts"],
timeseries_columns=["feature_ts"],
df=activity_features_df,
)
lookup = FeatureLookup(
table_name="main.ml.user_activity_features",
lookup_key="user_id",
timestamp_lookup_key="event_ts",
)
training_set = fe.create_training_set(
df=labeled_events_df,
feature_lookups=[lookup],
label="label",
)For each event, the lookup selects the latest feature record at or before event_ts.
Package features with the model
Log the training set with the model. Its feature lookup metadata records how features were joined, so inference can repeat the same retrieval.
import mlflow
mlflow.set_registry_uri("databricks-uc")
fe.log_model(
model=model,
artifact_path="model",
flavor=mlflow.sklearn,
training_set=training_set,
registered_model_name="main.ml.churn_model",
)Batch scoring
Batch scoring can supply only the lookup key. The model retrieves its registered features automatically.
batch_df = spark.table("main.ml.users_to_score").select("user_id")
predictions = fe.score_batch(
model_uri="models:/main.ml.churn_model/1",
df=batch_df,
)Operate features in production
Schedule feature computation with Databricks Jobs or Workflows. Monitor freshness, failed runs, row counts, and schema changes. A feature pipeline should publish data only after its upstream inputs have completed.
Online serving stores selected feature values for low-latency requests. Use it when an application needs predictions during a request. Keep online values synchronized with the governed feature table and test stale-data behavior.
Use Unity Catalog to discover tables, owners, descriptions, permissions, and lineage. Lineage helps trace a feature from its source tables through transformations to a model.
| Practice | Reason |
|---|---|
| Keep stable primary keys | Prevent ambiguous feature joins |
| Record feature timestamps | Enable point-in-time correctness |
| Document freshness | Set clear serving expectations |
Exercise
A feature row was created after a labeled event. Should a point-in-time lookup use it?
Show answer
No. It must use the latest row available at or before the event timestamp.