Sub topic 2
Data Sources, Feature Views and Training Sets in Feast
This chapter assumes you know Python and pandas, can run commands in a terminal, and understand that a model trains on rows of features paired with labels. Install Feast with pip install feast before you start.
Data sources and event timestamps
A data source is the file or table that holds feature values. Each row needs an entity key, such as driver_id, and an event timestamp that records when the value became true. The event timestamp lets Feast answer "what was this value at time T?" An optional created timestamp records when the row was written. Feast uses it to break ties when two rows share an event time.
Defining feature views
A feature view attaches feature columns from one source to one or more entities. It also sets a TTL (time to live), which limits how far back Feast looks for a value. The following file defines an entity, a Parquet source and a feature view.
from datetime import timedelta
from feast import Entity, FeatureView, Field, FileSource
from feast.types import Float32, Int64
driver = Entity(name="driver", join_keys=["driver_id"])
driver_stats_source = FileSource(
name="driver_hourly_stats_source",
path="data/driver_stats.parquet",
timestamp_field="event_timestamp",
created_timestamp_column="created",
)
driver_stats_fv = FeatureView(
name="driver_hourly_stats",
entities=[driver],
ttl=timedelta(days=1),
schema=[
Field(name="conv_rate", dtype=Float32),
Field(name="acc_rate", dtype=Float32),
Field(name="avg_daily_trips", dtype=Int64),
],
online=True,
source=driver_stats_source,
)The feature repository and feast apply
Feast reads definitions from Python files in a feature repository directory. A feature_store.yaml file in that directory names the registry, which is the metadata store that Feast reads later.
project: driver_project
registry: data/registry.db
provider: local
online_store:
type: sqlite
path: data/online_store.db
offline_store:
type: fileRun feast apply in the repository directory. It writes the entity, source and feature view definitions into the registry.
feast applyPoint-in-time correct joins and label leakage
A training row carries an entity key and a label time. A point-in-time join picks, for each row, the latest feature value at or before that time. If a join uses a value recorded after the label time, the model learns from the future. This is label leakage. It inflates offline metrics, and the model then underperforms in production.
Building a training set with get_historical_features
Pass get_historical_features a DataFrame of entity keys and label timestamps. Feast returns the feature values as they were at each row's timestamp.
from datetime import datetime
import pandas as pd
from feast import FeatureStore
store = FeatureStore(repo_path=".")
entity_df = pd.DataFrame(
{
"driver_id": [1001, 1002],
"event_timestamp": [datetime(2025, 1, 1, 12, 0), datetime(2025, 1, 1, 13, 0)],
}
)
training_df = store.get_historical_features(
entity_df=entity_df,
features=[
"driver_hourly_stats:conv_rate",
"driver_hourly_stats:acc_rate",
],
).to_df()Exercise
Exercise
A feature value for driver 1001 has eventtimestamp 14:00. Your entity row for that driver has eventtimestamp 13:00. Does gethistoricalfeatures use the 14:00 value for that row?
Show answer
No. The point-in-time join uses only values at or before the entity row's timestamp, so the 14:00 value is excluded. Feast returns the latest earlier value instead, or null if none falls within the TTL.