Skip to main content

Chapter 2: Feature Engineering at Scale

Feature Stores​

Feature stores centralize feature computation and serving, ensuring consistency between training and inference.

Feast Example​

from feast import FeatureStore, Entity, Feature, ValueType, FeatureView
from feast.infra.offline_stores.file_source import FileSource

# Define entity
customer = Entity(
name="customer_id",
value_type=ValueType.STRING,
description="Customer identifier"
)

# Define feature view
customer_features = FeatureView(
name="customer_features",
entities=["customer_id"],
ttl=timedelta(days=1),
features=[
Feature(name="total_purchases", dtype=ValueType.FLOAT),
Feature(name="days_since_last_login", dtype=ValueType.INT64),
Feature(name="account_age_days", dtype=ValueType.INT64),
],
online=True,
source=FileSource(path="data/customer_features.parquet")
)

# Get features for training
store = FeatureStore(repo_path=".")
training_df = store.get_historical_features(
entity_df=entity_df,
features=["customer_features:total_purchases", "customer_features:days_since_last_login"]
).to_df()

# Get features for inference (online)
online_features = store.get_online_features(
features=["customer_features:total_purchases"],
entity_rows=[{"customer_id": "CUS-12345"}]
).to_dict()

Online vs Offline Features​

TypePurposeLatencyStorage
OfflineTrainingMinutesData warehouse
OnlineInferenceMillisecondsRedis, DynamoDB

Key Takeaways​

Summary
  1. Feature stores ensure training-serving consistency
  2. Separate online/offline stores for different latency needs
  3. Version features like code and data

Next: Chapter 3: Data Quality and Validation