Skip to main content
Version: Beta 🚧

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)
amttransaction_amount_is_high__transaction_amount_is_high
0200True
120False

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_idamt_sum_1h_10mamt_sum_1d_10mamt_sum_3d_10mamt_mean_1h_10mamt_mean_1d_10mamt_mean_3d_10m_valid_from_valid_to
0user_1120.66120.661404.69120.66120.66351.1732022-05-01 00:00:002022-05-02 00:00:00
1user_2134.73134.73144.07134.73134.7372.0352022-05-01 00:00:002022-05-02 00:00:00
2user_376.4576.45181.476.4576.4560.46672022-05-01 00:00:002022-05-02 00:00:00
3user_32.1378.58183.532.1339.2945.88252022-05-02 00:00:002022-05-03 00:00:00
4user_39.9688.54193.499.9629.513338.6982022-05-03 00:00:002022-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_idtimestampamt
0user_22022-05-01 19:51:0071.82
1user_32022-05-02 05:00:0080.98
2user_12022-05-01 21:11:00400.55
3user_32022-05-01 07:21:0066.57
result_dataframe = fv.get_features_for_events(events=events, from_source=True).to_pandas()
display(result_dataframe)
user_idtimestampamttransaction_amount_is_higher_than_average__transaction_amount_is_higher_than_average
0user_22022-05-01 19:51:0071.820
1user_32022-05-02 05:00:0080.981
2user_12022-05-01 21:11:00400.551
3user_32022-05-03 07:21:0026.570

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}

Was this page helpful?

🧠 Hi! Ask me anything about Tecton!

Floating button icon