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 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)
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 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_idtimestampamt_sum_1h_10mamt_sum_1d_10mamt_sum_3d_10mamt_mean_1h_10mamt_mean_1d_10mamt_mean_3d_10m_effective_timestamp
0user_2225067899842022-05-01 21:10:00120.66120.661404.69120.66120.66351.1732022-05-01 21:10:00
1user_269908169682022-05-01 19:50:00134.73134.73144.07134.73134.7372.0352022-05-01 19:50:00
2user_3377503174122022-05-01 02:00:0076.4576.45181.476.4576.4560.46672022-05-01 02:00:00
3user_3377503174122022-05-01 07:20:002.1378.58183.532.1339.2945.88252022-05-01 07:20:00
4user_3377503174122022-05-01 14:10:009.9688.54193.499.9629.513338.6982022-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_idtimestampamt
0user_269908169682022-05-01 19:51:0071.82
1user_3377503174122022-05-02 05:00:0080.98
2user_2225067899842022-05-01 21:11:00180.55
3user_3377503174122022-05-01 07:21:0066.57
result_dataframe = fv.get_historical_features(spine=spine_df, from_source=True).to_pandas()
display(result_dataframe)
user_idtimestampamttransaction_amount_is_higher_than_average__transaction_amount_is_higher_than_average
0user_269908169682022-05-01 19:51:0071.820
1user_3377503174122022-05-02 05:00:0080.981
2user_2225067899842022-05-01 21:11:00180.551
3user_3377503174122022-05-01 07:21:0066.571

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