Sub topic 9
Defining feature lookups
This chapter assumes you can write PySpark DataFrame code, know that a feature table is a Unity Catalog Delta table with a primary key, and have already created main.ml.user_features. A feature lookup is the declaration that connects a feature table to a labeled DataFrame. The next chapter uses these lookups to build training sets.
What a lookup declares
A FeatureLookup does not read any data. It is a description of a join: which table to read, which columns to take, and which column in your labeled data matches the table's primary key. The join runs later, when you call create_training_set.
from databricks.feature_engineering import FeatureLookup
user_lookup = FeatureLookup(
table_name="main.ml.user_features",
feature_names=["order_count_90d", "avg_order_amount_90d"],
lookup_key="user_id",
)The three arguments do the following:
table_nameis the three-level Unity Catalog name of the feature table.feature_nameslists the columns to retrieve. If you leave it out, every column except the primary keys is returned.lookup_keyis the column in the labeled DataFrame that is matched against the table's primary key.
Matching keys
The lookup key must correspond to the primary key of the feature table. For a table keyed by user_id, your labeled DataFrame needs a user_id column. If the column has a different name in your labels, pass that name instead. For a composite key, pass a list with one column per primary key column, in the same order as the table's key.
store_lookup = FeatureLookup(
table_name="main.ml.store_features",
lookup_key=["store_id", "region_id"],
)Several lookups in one training set
You can combine features from many tables by building a list of lookups. Each lookup joins independently on its own key.
feature_lookups = [
FeatureLookup(
table_name="main.ml.user_features",
feature_names=["order_count_90d"],
lookup_key="user_id",
),
FeatureLookup(
table_name="main.ml.product_features",
feature_names=["avg_rating"],
lookup_key="product_id",
),
]Both user_id and product_id must exist in the labeled DataFrame.
Common errors
- Name collisions. If two tables contain a feature with the same name, or a feature name matches a column already in the labels, the join is ambiguous. Use the
output_nameargument to rename a feature in the result. - Missing keys. A label row with no matching key in the feature table gets null feature values. Check for nulls before training.
- Wrong key type. A
user_idstored as a string in one place and a bigint in the other will not match as intended. Cast to the table's type first. - Time series tables. If the feature table has a time series column, add
timestamp_lookup_key. This is covered in the point-in-time chapter.
Exercise
A labeled DataFrame has columns customer_id, order_id, and label. The feature table main.ml.customer_features has primary key cust_id. How do you write a lookup that retrieves lifetime_value?
Show answer
Pass the label column name as the lookup key: FeatureLookup(table_name="main.ml.customer_features", feature_names=["lifetime_value"], lookup_key="customer_id"). Rows are matched to the table's primary key cust_id by position, so the names do not have to be the same.