Test On-Demand Features
Import libraries and select your workspaceโ
tip
You will need an API Key to proceed.
import tecton
import pandas
from datetime import datetime
ws = tecton.get_workspace("prod")
On-Demand Feature Views with no Feature View Dependenciesโ
Load an On-Demand Feature Viewโ
fv = ws.get_feature_view("transaction_amount_is_high")
fv.summary()
Execute an On-Demand Feature View Onlineโ
Because this On-Demand Feature View has no Batch or Stream Feature View dependencies it only needs request data to execute.
fv.get_online_features(request_data={"amt": 17000}).to_dict()
Out: {"transaction_amount_is_high": True}
Execute an On-Demand Feature View Offlineโ
Create a spine DataFrame with the request data to transform.
spine_df = pandas.DataFrame({"amt": [200, 20]})
features_df = fv.get_historical_features(spine=spine_df).to_pandas()
display(features_df)
| amt | transaction_amount_is_high__transaction_amount_is_high | |
|---|---|---|
| 0 | 200 | True |
| 1 | 20 | False |
On-Demand Feature Views with Feature View Dependenciesโ
Load an On-Demand Feature Viewโ
fv = ws.get_feature_view("transaction_amount_is_higher_than_average")
Run Feature View Transformation Pipeline with Mock Inputsโ
To use run on On-Demand Feature View, all Feature View inputs must be mocked.
transaction_request = pandas.DataFrame([{"amt": 100.0}])
user_transaction_amount_metrics = pandas.DataFrame([{"amt_mean_1d_10m": 200.0}])
result = fv.run(
transaction_request=transaction_request,
user_transaction_amount_metrics=user_transaction_amount_metrics,
)
print(result)
{"transaction_amount_is_higher_than_average": 0}
Execute On-Demand Feature View with Dependencies Offlineโ
Because this On-Demand Feature View depends on another Feature View, we will first preview the dependent Feature View. We can use this to select keys that will be needed for historical lookups in order for the On-Demand Feature View to run. The dependent column in the Feature View below is amt_mean_1d_10m.
dependent_fv = ws.get_feature_view("user_transaction_amount_metrics")
result_dataframe = dependent_fv.get_historical_features(
start_time=datetime(2022, 5, 1), end_time=datetime(2022, 5, 2), from_source=True
).to_pandas()
display(result_dataframe)
| user_id | timestamp | amt_sum_1h_10m | amt_sum_1d_10m | amt_sum_3d_10m | amt_mean_1h_10m | amt_mean_1d_10m | amt_mean_3d_10m | _effective_timestamp | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | user_222506789984 | 2022-05-01 21:10:00 | 120.66 | 120.66 | 1404.69 | 120.66 | 120.66 | 351.173 | 2022-05-01 21:10:00 |
| 1 | user_26990816968 | 2022-05-01 19:50:00 | 134.73 | 134.73 | 144.07 | 134.73 | 134.73 | 72.035 | 2022-05-01 19:50:00 |
| 2 | user_337750317412 | 2022-05-01 02:00:00 | 76.45 | 76.45 | 181.4 | 76.45 | 76.45 | 60.4667 | 2022-05-01 02:00:00 |
| 3 | user_337750317412 | 2022-05-01 07:20:00 | 2.13 | 78.58 | 183.53 | 2.13 | 39.29 | 45.8825 | 2022-05-01 07:20:00 |
| 4 | user_337750317412 | 2022-05-01 14:10:00 | 9.96 | 88.54 | 193.49 | 9.96 | 29.5133 | 38.698 | 2022-05-01 14:10:00 |
Create a spine DataFrame with events to look up. For more information on
spines, check out
Selecting Sample Keys and Timestamps.
spine_df = pandas.DataFrame(
{
"user_id": [
"user_26990816968",
"user_337750317412",
"user_222506789984",
"user_337750317412",
],
"timestamp": [
datetime(2022, 5, 1, 19, 51),
datetime(2022, 5, 2, 5, 0),
datetime(2022, 5, 1, 21, 11),
datetime(2022, 5, 1, 7, 21),
],
"amt": [71.82, 80.98, 180.55, 66.57],
}
)
display(spine_df)
| user_id | timestamp | amt | |
|---|---|---|---|
| 0 | user_26990816968 | 2022-05-01 19:51:00 | 71.82 |
| 1 | user_337750317412 | 2022-05-02 05:00:00 | 80.98 |
| 2 | user_222506789984 | 2022-05-01 21:11:00 | 180.55 |
| 3 | user_337750317412 | 2022-05-01 07:21:00 | 66.57 |
result_dataframe = fv.get_historical_features(spine=spine_df, from_source=True).to_pandas()
display(result_dataframe)
| user_id | timestamp | amt | transaction_amount_is_higher_than_average__transaction_amount_is_higher_than_average | |
|---|---|---|---|---|
| 0 | user_26990816968 | 2022-05-01 19:51:00 | 71.82 | 0 |
| 1 | user_337750317412 | 2022-05-02 05:00:00 | 80.98 | 1 |
| 2 | user_222506789984 | 2022-05-01 21:11:00 | 180.55 | 1 |
| 3 | user_337750317412 | 2022-05-01 07:21:00 | 66.57 | 1 |
Execute On-Demand Feature View with Dependencies Onlineโ
# This will compare a transaction amount against the user's most recent average 24h transaction amount.
fv.get_online_features(request_data={"amt": 150}, join_keys={"user_id": "user_724235628997"}).to_dict()
Out: {"transaction_amount_is_higher_than_average": 1}