On-Demand Feature Usage¶
In [0]:
Copied!
import tecton
import pandas
from datetime import datetime
ws = tecton.get_workspace("prod")
import tecton
import pandas
from datetime import datetime
ws = tecton.get_workspace("prod")
On-Demand Feature Views without Dependencies¶
Load an On-Demand Feature View¶
In [0]:
Copied!
fv = ws.get_feature_view("transaction_amount_is_high")
fv.summary()
fv = ws.get_feature_view("transaction_amount_is_high")
fv.summary()
Out[3]:
Name | transaction_amount_is_high |
Workspace | prod |
Description | The transaction amount is higher than $100. |
Created At | 2022-06-06 23:13:49 UTC |
Owner | |
Last Modified By | jake@tecton.ai |
Family | |
Source Filename | fraud/features/on_demand_feature_views/transaction_amount_is_high.py |
Tags | {} |
Type | OnDemandFeatureView |
URL | https://staging.tecton.ai/app/repo/prod/features/transaction_amount_is_high |
Entities | |
Features | transaction_amount_is_high |
Transformation | transaction_amount_is_high |
Join Keys | |
Request Context Keys | amt |
Execute an On-Demand Feature View Online¶
Because this On-Demand Feature View has no dependencies it only needs request data to execute
In [0]:
Copied!
fv.get_online_features(request_data={"amt": 17000}).to_dict()
fv.get_online_features(request_data={"amt": 17000}).to_dict()
Out[8]: {'transaction_amount_is_high': True}
Execute On-Demand Feature View Offline¶
In [0]:
Copied!
spine_df = pandas.DataFrame({'amt': [200, 20]})
features_df = fv.get_historical_features(spine=spine_df).to_pandas()
display(features_df)
spine_df = pandas.DataFrame({'amt': [200, 20]})
features_df = fv.get_historical_features(spine=spine_df).to_pandas()
display(features_df)
INFO - 06/07/2022 06:00:58 PM - Query Builder - transaction_amount_is_high: (FV 1/1) Start Build [1/3]
INFO - 06/07/2022 06:00:58 PM - Query Builder - transaction_amount_is_high: Building Tile DataFrame [2/3]
amt | transaction_amount_is_high__transaction_amount_is_high |
---|---|
200 | true |
20 | false |
On-Demand Feature Views with Feature View Dependencies¶
Load an On-Demand Feature View¶
In [0]:
Copied!
fv = ws.get_feature_view("transaction_amount_is_higher_than_average")
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.
In [0]:
Copied!
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_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 False
dtype: bool}
Execute On-Demand FeatureView 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
.
In [0]:
Copied!
dependent_fv = ws.get_feature_view("user_transaction_amount_metrics")
dependent_fv.get_historical_features(start_time=datetime(2022, 5, 1), end_time=datetime(2022, 5, 2)).to_pandas().sample(4)
dependent_fv = ws.get_feature_view("user_transaction_amount_metrics")
dependent_fv.get_historical_features(start_time=datetime(2022, 5, 1), end_time=datetime(2022, 5, 2)).to_pandas().sample(4)
Out[21]:
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 | timestamp | |
---|---|---|---|---|---|---|---|---|
4350 | user_724235628997 | 1866.68 | 82881.33 | 231085.28 | 46.667000 | 71.820910 | 64.548961 | 2022-05-01 08:20:00 |
3452 | user_650387977076 | 3803.60 | 123012.79 | 360482.51 | 63.393333 | 80.982745 | 78.331706 | 2022-05-01 20:10:00 |
5261 | user_916905857181 | 857.92 | 31343.50 | 96362.05 | 71.493333 | 95.559451 | 96.073829 | 2022-05-01 10:20:00 |
483 | user_222506789984 | 7054.43 | 140996.11 | 424768.41 | 69.845842 | 66.570401 | 67.713759 | 2022-05-01 19:00:00 |
In [0]:
Copied!
spine_df = pandas.DataFrame({
'user_id': ["user_724235628997", "user_650387977076", "user_916905857181", "user_222506789984"],
'timestamp': [datetime(2022, 5, 1, 8, 20), datetime(2022, 5, 1, 20, 10), datetime(2022, 5, 1, 10, 20), datetime(2022, 5, 1, 19, 0),],
'amt': [(71.820910 + 10), (80.982745 - 10), (95.559451 + 10), (66.570401 - 10)]
})
spine_df.head()
spine_df = pandas.DataFrame({
'user_id': ["user_724235628997", "user_650387977076", "user_916905857181", "user_222506789984"],
'timestamp': [datetime(2022, 5, 1, 8, 20), datetime(2022, 5, 1, 20, 10), datetime(2022, 5, 1, 10, 20), datetime(2022, 5, 1, 19, 0),],
'amt': [(71.820910 + 10), (80.982745 - 10), (95.559451 + 10), (66.570401 - 10)]
})
spine_df.head()
Out[24]:
user_id | timestamp | amt | |
---|---|---|---|
0 | user_724235628997 | 2022-05-01 08:20:00 | 81.820910 |
1 | user_650387977076 | 2022-05-01 20:10:00 | 70.982745 |
2 | user_916905857181 | 2022-05-01 10:20:00 | 105.559451 |
3 | user_222506789984 | 2022-05-01 19:00:00 | 56.570401 |
In [0]:
Copied!
fv.get_historical_features(spine=spine_df).to_pandas()
fv.get_historical_features(spine=spine_df).to_pandas()
INFO - 06/07/2022 06:11:55 PM - Query Builder - user_transaction_amount_metrics: (FV 1/2) Start Build [1/3]
INFO - 06/07/2022 06:11:55 PM - Query Builder - user_transaction_amount_metrics: Building Tile DataFrame [2/3]
INFO - 06/07/2022 06:12:13 PM - Query Builder - user_transaction_amount_metrics: Generating Join [3/3]
INFO - 06/07/2022 06:12:13 PM - Query Builder - transaction_amount_is_higher_than_average: (FV 2/2) Start Build [1/3]
INFO - 06/07/2022 06:12:13 PM - Query Builder - transaction_amount_is_higher_than_average: Building Tile DataFrame [2/3]
Out[25]:
user_id | timestamp | amt | transaction_amount_is_higher_than_average__transaction_amount_is_higher_than_average | |
---|---|---|---|---|
0 | user_724235628997 | 2022-05-01 08:20:00 | 81.820910 | True |
1 | user_650387977076 | 2022-05-01 20:10:00 | 70.982745 | False |
2 | user_916905857181 | 2022-05-01 10:20:00 | 105.559451 | True |
3 | user_222506789984 | 2022-05-01 19:00:00 | 56.570401 | False |
Execute On-Demand FeatureView with Dependencies Online¶
In [0]:
Copied!
# 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()
# 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[26]: {'transaction_amount_is_higher_than_average': True}