On-Demand Feature Usage¶
In [0]:
Copied!
import tecton
import pandas
from datetime import datetime
import dateutil.parser
import tecton
import pandas
from datetime import datetime
import dateutil.parser
On-Demand Feature Views without Dependencies¶
Load an On-Demand Feature View¶
In [0]:
Copied!
fv = tecton.get_feature_view("transaction_amount_is_high")
fv.summary()
fv = tecton.get_feature_view("transaction_amount_is_high")
fv.summary()
Name | transaction_amount_is_high |
Workspace | prod |
Description | Whether the transaction amount is considered high (over $10000) |
Created At | 2021-06-24 14:34:27 UTC |
Owner | matt@tecton.ai |
Last Modified By | kevin@tecton.ai |
Family | fraud |
Source Filename | fraud/features/on_demand_feature_views/transaction_amount_is_high.py |
Tags | {'release': 'production'} |
Type | OnDemandFeatureView |
URL | https://app.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 | amount |
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={"amount": 17000}).to_dict()
fv.get_online_features(request_data={"amount": 17000}).to_dict()
Out[3]: {'transaction_amount_is_high': 1}
Execute On-Demand Feature View Offline¶
In [0]:
Copied!
spine_df = pandas.DataFrame({'amount': [20000, 1000]})
features_df = fv.get_historical_features(spine=spine_df).to_pandas()
display(features_df)
spine_df = pandas.DataFrame({'amount': [20000, 1000]})
features_df = fv.get_historical_features(spine=spine_df).to_pandas()
display(features_df)
amount | transaction_amount_is_high.transaction_amount_is_high |
---|---|
20000 | 1 |
1000 | 0 |
On-Demand Feature Views with Feature View Dependencies¶
Load an On-Demand Feature View¶
In [0]:
Copied!
fv = tecton.get_feature_view("transaction_amount_is_higher_than_average")
fv = tecton.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 [ ]:
Copied!
transaction_request = pandas.DataFrame([{
"amount": 100.0
}])
user_transaction_amount_metrics = pandas.DataFrame([{
"amount_mean_24h_10m": 200.0
}])
result_dataframe = fv.run(transaction_request=transaction_request, user_transaction_amount_metrics=user_transaction_amount_metrics)
display(result_dataframe.to_spark())
transaction_request = pandas.DataFrame([{
"amount": 100.0
}])
user_transaction_amount_metrics = pandas.DataFrame([{
"amount_mean_24h_10m": 200.0
}])
result_dataframe = fv.run(transaction_request=transaction_request, user_transaction_amount_metrics=user_transaction_amount_metrics)
display(result_dataframe.to_spark())
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 amount_sum_24h_10m
.
In [0]:
Copied!
dependent_fv = tecton.get_feature_view("user_transaction_amount_metrics")
dependent_fv.get_historical_features(start_time=datetime(2021, 1, 1), end_time=datetime(2021, 6, 1)).to_pandas().head()
dependent_fv = tecton.get_feature_view("user_transaction_amount_metrics")
dependent_fv.get_historical_features(start_time=datetime(2021, 1, 1), end_time=datetime(2021, 6, 1)).to_pandas().head()
user_id | amount_mean_1h_10m | amount_mean_12h_10m | amount_mean_24h_10m | amount_mean_72h_10m | amount_sum_1h_10m | amount_sum_12h_10m | amount_sum_24h_10m | amount_sum_72h_10m | timestamp | |
---|---|---|---|---|---|---|---|---|---|---|
0 | C1000262126 | 92437.11 | 92437.11 | 92437.11 | 92437.11 | 92437.11 | 92437.11 | 92437.11 | 92437.11 | 2021-05-29 19:10:00 |
1 | C1003164154 | 11877.38 | 11877.38 | 11877.38 | 11877.38 | 11877.38 | 11877.38 | 11877.38 | 11877.38 | 2021-05-28 21:10:00 |
2 | C1003164154 | 5913202.56 | 5913202.56 | 5913202.56 | 2962539.97 | 5913202.56 | 5913202.56 | 5913202.56 | 5925079.94 | 2021-05-29 21:30:00 |
3 | C1003164154 | 281522.05 | 281522.05 | 281522.05 | 2068867.33 | 281522.05 | 281522.05 | 281522.05 | 6206601.99 | 2021-05-31 13:40:00 |
4 | C1005557457 | 82482.13 | 82482.13 | 82482.13 | 82482.13 | 82482.13 | 82482.13 | 82482.13 | 82482.13 | 2021-05-28 18:00:00 |
In [0]:
Copied!
spine_df = pandas.DataFrame({
'user_id': ["C1003164154", "C1003164154", "C1003164154", "C1003164154"],
'timestamp': [
dateutil.parser.parse("2021-05-28 23:17:00"),
dateutil.parser.parse("2021-05-28 23:17:00"),
dateutil.parser.parse("2021-05-31 17:00:00"),
dateutil.parser.parse("2021-05-31 17:00:00"),],
'amount': [(11877.38 + 10), (11877.38 - 10), (281522.05 + 10), (281522.05 - 10)]
})
spine_df.head()
spine_df = pandas.DataFrame({
'user_id': ["C1003164154", "C1003164154", "C1003164154", "C1003164154"],
'timestamp': [
dateutil.parser.parse("2021-05-28 23:17:00"),
dateutil.parser.parse("2021-05-28 23:17:00"),
dateutil.parser.parse("2021-05-31 17:00:00"),
dateutil.parser.parse("2021-05-31 17:00:00"),],
'amount': [(11877.38 + 10), (11877.38 - 10), (281522.05 + 10), (281522.05 - 10)]
})
spine_df.head()
user_id | timestamp | amount | |
---|---|---|---|
0 | C1003164154 | 2021-05-28 23:17:00 | 11887.38 |
1 | C1003164154 | 2021-05-28 23:17:00 | 11867.38 |
2 | C1003164154 | 2021-05-31 17:00:00 | 281532.05 |
3 | C1003164154 | 2021-05-31 17:00:00 | 281512.05 |
In [0]:
Copied!
fv.get_historical_features(spine=spine_df, timestamp_key="timestamp").to_pandas()
fv.get_historical_features(spine=spine_df, timestamp_key="timestamp").to_pandas()
user_id | timestamp | amount | transaction_amount_is_higher_than_average.transaction_amount_is_higher_than_average | |
---|---|---|---|---|
0 | C1003164154 | 2021-05-28 23:17:00 | 11887.38 | 1 |
1 | C1003164154 | 2021-05-28 23:17:00 | 11867.38 | 0 |
2 | C1003164154 | 2021-05-31 17:00:00 | 281532.05 | 1 |
3 | C1003164154 | 2021-05-31 17:00:00 | 281512.05 | 0 |
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={"amount": 150}, join_keys={"user_id": "C1003164154"}).to_dict()
# This will compare a transaction amount against the user's most recent average 24h transaction amount
fv.get_online_features(request_data={"amount": 150}, join_keys={"user_id": "C1003164154"}).to_dict()
Out[9]: {'transaction_amount_is_higher_than_average': 1}