Snowpark ML Feature Store
A Feature Store provides a centralized, reusable repository of curated features for machine learning. Snowpark ML Feature Store brings this capability natively into Snowflake, eliminating the need for external feature platforms.
Why a Feature Store Matters
Without a feature store, teams duplicate feature logic across notebooks and pipelines. This causes inconsistencies between training and serving, wasted compute, and difficulty auditing which features a model was trained on.
| Problem | Feature Store Solution |
|---|---|
| Duplicated feature SQL across teams | Single source of truth for feature definitions |
| Training/serving skew | Same feature pipeline for both contexts |
| No lineage or audit trail | Metadata tracks feature provenance |
| Recomputing features per model | Precomputed features shared across models |
Core Concepts
| Concept | Description |
|---|---|
| Entity | A business object with a unique key (e.g., customer, product) |
| Feature View | A named set of features tied to an entity, backed by a query |
| Feature Store | The schema-level container managing entities and feature views |
| Spine | A DataFrame of entity keys and timestamps used to retrieve features |
Setting Up the Feature Store
from snowflake.ml.feature_store import FeatureStore, Entity, FeatureView
fs = FeatureStore(
session=session,
database="ML_DB",
name="FEATURE_STORE",
default_warehouse="ML_WH"
)
Creating Entities
An entity defines the join key for a group of features.
customer_entity = Entity(
name="CUSTOMER",
join_keys=["CUSTOMER_ID"]
)
fs.register_entity(customer_entity)
# Compound key entity
order_entity = Entity(
name="ORDER_ITEM",
join_keys=["ORDER_ID", "LINE_ITEM_ID"]
)
fs.register_entity(order_entity)
Creating Feature Views
A Feature View defines a set of features as a Snowpark DataFrame transformation.
from snowflake.snowpark.functions import col, sum as sp_sum, count, avg, datediff, current_date
# Define the feature query
customer_features_df = session.table("ORDERS").group_by("CUSTOMER_ID").agg(
sp_sum("ORDER_TOTAL").alias("LIFETIME_SPEND"),
count("ORDER_ID").alias("TOTAL_ORDERS"),
avg("ORDER_TOTAL").alias("AVG_ORDER_VALUE")
)
# Register as a Feature View
customer_fv = FeatureView(
name="CUSTOMER_SPEND_FEATURES",
entities=[customer_entity],
feature_df=customer_features_df,
refresh_freq="1 day",
description="Aggregated spending features per customer"
)
customer_fv = fs.register_feature_view(
feature_view=customer_fv,
version="v1"
)
Feature View Refresh Modes
| Mode | refresh_freq Value | Use Case |
|---|---|---|
| Scheduled | "1 hour", "1 day" | Features that update periodically |
| Static | None | One-time snapshot features |
Generating Training Datasets
Use a spine DataFrame containing entity keys and timestamps to retrieve point-in-time correct features.
# Spine: the set of training examples with their event timestamps
spine_df = session.table("TRAINING_EVENTS").select(
col("CUSTOMER_ID"),
col("EVENT_TIMESTAMP"),
col("CHURN_LABEL")
)
# Generate training dataset by joining features to the spine
training_data = fs.generate_dataset(
name="CHURN_TRAINING_SET",
version="v1",
spine_df=spine_df,
features=[customer_fv],
spine_timestamp_col="EVENT_TIMESTAMP"
)
training_df = training_data.read.to_snowpark_dataframe()
training_df.show()
Point-in-Time Lookups
Point-in-time correctness prevents data leakage by ensuring each training example only sees features available at the time of its event. The spine_timestamp_col parameter drives this behavior — the Feature Store joins on entity keys and filters features to those computed before each event timestamp.
Timeline:
Feature computed at T1 ──────────> Feature computed at T2
^
Training event at T1.5
(only sees T1 features)
Retrieving Features for Inference
At inference time, retrieve the latest feature values for a set of entity keys.
inference_spine = session.create_dataframe(
[[1001], [1002], [1003]],
schema=["CUSTOMER_ID"]
)
inference_features = fs.retrieve_feature_values(
spine_df=inference_spine,
features=[customer_fv]
)
inference_features.show()
Feature Reuse Across Models
Multiple models can reference the same Feature View. This ensures consistent feature definitions and eliminates redundant computation.
# Churn model uses customer spend features
churn_data = fs.generate_dataset(
name="CHURN_DS", version="v1",
spine_df=churn_spine, features=[customer_fv]
)
# LTV model reuses the same features plus additional ones
ltv_data = fs.generate_dataset(
name="LTV_DS", version="v1",
spine_df=ltv_spine, features=[customer_fv, demographic_fv]
)
Listing and Managing Feature Views
# List all feature views
fs.list_feature_views().show()
# Get a specific feature view
fv = fs.get_feature_view("CUSTOMER_SPEND_FEATURES", "v1")
# List all entities
fs.list_entities().show()
# Delete a feature view
fs.delete_feature_view(fv)
Access Control
| Operation | Required Privilege |
|---|---|
| Register entity/feature view | CREATE DYNAMIC TABLE on schema |
| Generate training dataset | SELECT on source tables |
| Retrieve features | USAGE on feature store schema |
Key Takeaways
- The Feature Store centralizes feature definitions so teams share a single source of truth.
- Point-in-time lookups prevent training/serving skew and data leakage.
- Feature Views are backed by scheduled queries, keeping features fresh automatically.
- Multiple models can reuse the same features without recomputing them.