Test On-Demand Features
Import libraries and select your workspace​
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 an events
DataFrame with the request data to transform.
events = pandas.DataFrame({"amt": [200, 20]})
features_df = fv.get_features_for_events(events=events).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_transformation()
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_transformation(
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_features_in_range(
start_time=datetime(2022, 5, 1), end_time=datetime(2022, 5, 4), from_source=True
).to_pandas()
display(result_dataframe)
user_id | amt_sum_1h_10m | amt_sum_1d_10m | amt_sum_3d_10m | amt_mean_1h_10m | amt_mean_1d_10m | amt_mean_3d_10m | _valid_from | _valid_to | |
---|---|---|---|---|---|---|---|---|---|
0 | user_1 | 120.66 | 120.66 | 1404.69 | 120.66 | 120.66 | 351.173 | 2022-05-01 00:00:00 | 2022-05-02 00:00:00 |
1 | user_2 | 134.73 | 134.73 | 144.07 | 134.73 | 134.73 | 72.035 | 2022-05-01 00:00:00 | 2022-05-02 00:00:00 |
2 | user_3 | 76.45 | 76.45 | 181.4 | 76.45 | 76.45 | 60.4667 | 2022-05-01 00:00:00 | 2022-05-02 00:00:00 |
3 | user_3 | 2.13 | 78.58 | 183.53 | 2.13 | 39.29 | 45.8825 | 2022-05-02 00:00:00 | 2022-05-03 00:00:00 |
4 | user_3 | 9.96 | 88.54 | 193.49 | 9.96 | 29.5133 | 38.698 | 2022-05-03 00:00:00 | 2022-05-04 00:00:00 |
Create an events
DataFrame with events to look up. For more information on the
events dataframe, check out
Selecting Sample Keys and Timestamps.
events = pandas.DataFrame(
{
"user_id": [
"user_2",
"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, 400.55, 66.57],
}
)
display(events)
user_id | timestamp | amt | |
---|---|---|---|
0 | user_2 | 2022-05-01 19:51:00 | 71.82 |
1 | user_3 | 2022-05-02 05:00:00 | 80.98 |
2 | user_1 | 2022-05-01 21:11:00 | 400.55 |
3 | user_3 | 2022-05-01 07:21:00 | 66.57 |
result_dataframe = fv.get_features_for_events(events=events, 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_2 | 2022-05-01 19:51:00 | 71.82 | 0 |
1 | user_3 | 2022-05-02 05:00:00 | 80.98 | 1 |
2 | user_1 | 2022-05-01 21:11:00 | 400.55 | 1 |
3 | user_3 | 2022-05-03 07:21:00 | 26.57 | 0 |
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}