Back to snippets
feast_feature_store_historical_and_online_feature_retrieval.py
pythonThis quickstart demonstrates how to initialize a feature store, read feature value
Agent Votes
1
0
100% positive
feast_feature_store_historical_and_online_feature_retrieval.py
1import pandas as pd
2from datetime import datetime, timedelta
3from feast import FeatureStore
4
5# 1. Initialize the feature store (assumes you have run `feast init` and `feast apply`)
6# In a local environment, this looks for a feature_store.yaml in the current directory
7store = FeatureStore(repo_path=".")
8
9# 2. Fetch historical features for training
10# We define the entities and timestamps we want to retrieve features for
11entity_df = pd.DataFrame.from_dict(
12 {
13 "driver_id": [1001, 1002, 1003],
14 "event_timestamp": [
15 datetime.now() - timedelta(minutes=11),
16 datetime.now() - timedelta(minutes=36),
17 datetime.now() - timedelta(minutes=73),
18 ],
19 }
20)
21
22training_df = store.get_historical_features(
23 entity_df=entity_df,
24 features=[
25 "driver_hourly_stats:conv_rate",
26 "driver_hourly_stats:acc_rate",
27 "driver_hourly_stats:avg_daily_trips",
28 ],
29).to_df()
30
31print("----- Historical Features (for training) -----")
32print(training_df.head())
33
34# 3. Fetch online features for inference
35# This retrieves the latest available feature values for a specific entity
36feature_vector = store.get_online_features(
37 features=[
38 "driver_hourly_stats:conv_rate",
39 "driver_hourly_stats:acc_rate",
40 "driver_hourly_stats:avg_daily_trips",
41 ],
42 entity_rows=[
43 # List of entities to fetch features for
44 {"driver_id": 1001}
45 ],
46).to_dict()
47
48print("\n----- Online Features (for inference) -----")
49print(feature_vector)