10. Derive Features from other Features
Derive feature from other features¶
Features can also be created based on other features.
In this tutorial we will create a feature that measures the Z-Score of the Latest invoice Amount for the customer compared to that customer's invoices over a 28d period. This feature is derived from 3 features we created previously: CUSTOMER_Latest_invoice_Amount, CUSTOMER_Avg_of_invoice_Amount_28d and CUSTOMER_Std_of_invoice_Amount_28d.
In [1]:
Copied!
import featurebyte as fb
# Set your profile to the tutorial environment
fb.use_profile("tutorial")
catalog_name = "Grocery Dataset Tutorial"
catalog = fb.Catalog.activate(catalog_name)
import featurebyte as fb
# Set your profile to the tutorial environment
fb.use_profile("tutorial")
catalog_name = "Grocery Dataset Tutorial"
catalog = fb.Catalog.activate(catalog_name)
10:51:19 | WARNING | Service endpoint is inaccessible: http://featurebyte-server:8088/ 10:51:19 | INFO | Using profile: tutorial 10:51:20 | INFO | Using configuration file at: /Users/gxav/.featurebyte/config.yaml 10:51:20 | INFO | Active profile: tutorial (https://tutorials.featurebyte.com/api/v1) 10:51:20 | INFO | SDK version: 2.0.1.dev67 10:51:20 | INFO | No catalog activated. 10:51:20 | INFO | Catalog activated: Grocery Dataset Tutorial 16:08:27 | INFO | Using configuration file at: /Users/gxav/.featurebyte/config.yaml 16:08:27 | INFO | Active profile: tutorial (https://tutorials.featurebyte.com/api/v1) 16:08:27 | WARNING | Remote SDK version (1.1.0.dev7) is different from local (1.1.0.dev1). Update local SDK to avoid unexpected behavior. 16:08:27 | INFO | No catalog activated. 16:08:27 | INFO | Catalog activated: Grocery Dataset Tutorial
Get features we previously created and saved¶
In [2]:
Copied!
customer_latest_invoice_amount = catalog.get_feature("CUSTOMER_Latest_invoice_Amount")
customer_avg_of_invoice_amount_28d = catalog.get_feature("CUSTOMER_Avg_of_invoice_Amount_28d")
customer_std_of_invoice_amount_28d = catalog.get_feature("CUSTOMER_Std_of_invoice_Amount_28d")
customer_latest_invoice_amount = catalog.get_feature("CUSTOMER_Latest_invoice_Amount")
customer_avg_of_invoice_amount_28d = catalog.get_feature("CUSTOMER_Avg_of_invoice_Amount_28d")
customer_std_of_invoice_amount_28d = catalog.get_feature("CUSTOMER_Std_of_invoice_Amount_28d")
Derive a z-score feature from saved features¶
In [3]:
Copied!
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d = (
customer_latest_invoice_amount
- customer_avg_of_invoice_amount_28d
) / customer_std_of_invoice_amount_28d
# Give a name to new feature
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.name = \
"CUSTOMER_Latest_invoice_Amount_Z_Score_to_invoice_Amount_28d"
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d = (
customer_latest_invoice_amount
- customer_avg_of_invoice_amount_28d
) / customer_std_of_invoice_amount_28d
# Give a name to new feature
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.name = \
"CUSTOMER_Latest_invoice_Amount_Z_Score_to_invoice_Amount_28d"
Preview feature¶
In [4]:
Copied!
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.primary_entity
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.primary_entity
Out[4]:
[<featurebyte.api.entity.Entity at 0x323466bb0> { 'name': 'customer', 'created_at': '2024-12-18T02:40:54.536000', 'updated_at': '2024-12-18T02:40:58.932000', 'description': None, 'serving_names': [ 'GROCERYCUSTOMERGUID' ], 'catalog_name': 'Grocery Dataset Tutorial' }]
In [5]:
Copied!
# Get observation table: 'Preview Table with 10 items'
preview_table = catalog.get_observation_table("Preview Table with 10 items")
# Get observation table: 'Preview Table with 10 items'
preview_table = catalog.get_observation_table("Preview Table with 10 items")
In [6]:
Copied!
# Preview CUSTOMER_Age
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.preview(preview_table)
# Preview CUSTOMER_Age
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.preview(preview_table)
Out[6]:
POINT_IN_TIME | GROCERYINVOICEITEMGUID | CUSTOMER_Latest_invoice_Amount_Z_Score_to_invoice_Amount_28d | |
---|---|---|---|
0 | 2022-07-23 17:33:25 | 849454c5-6640-419d-871d-1f0895a1c3b4 | NaN |
1 | 2023-01-06 08:49:29 | dad86e21-3af4-4b5b-8058-60f946b6dac5 | 0.016777 |
2 | 2023-06-06 16:13:44 | bdc6b6bb-a23a-48c8-bb3a-622d9161c0e8 | 1.008156 |
3 | 2022-09-07 09:16:15 | 53eac49e-7ecd-4cb6-a1c0-38e3900efd7a | 0.901901 |
4 | 2023-05-21 13:59:44 | 989643fe-1377-4f5f-8f38-f349a611da0c | -1.404144 |
5 | 2022-08-22 06:10:42 | 9267d0dd-9685-4667-8f06-1761abe73c4d | -0.820547 |
6 | 2023-03-07 17:53:55 | cb478a4e-9266-4523-8ee0-e205881cc5f5 | NaN |
7 | 2022-12-30 16:42:05 | 4dd1487a-0379-4eef-b200-97ac1bb1164f | 1.340493 |
8 | 2022-09-23 18:09:33 | 459a1b6e-1239-46d1-9e40-539c7e895483 | -0.181665 |
9 | 2023-03-21 13:49:48 | 84225bbb-adb8-451e-98c0-897c83c2fad9 | -0.063562 |
Save feature¶
In [7]:
Copied!
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.save()
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.save()
Done! |████████████████████████████████████████| 100% in 6.1s (0.17%/s) Done! |████████████████████████████████████████| 100% in 6.1s (0.17%/s)
Add description¶
In [8]:
Copied!
# Add description
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.update_description(
"Z-Score of the Latest invoice Amount for the customer compared to customer invoices over a 28d period"
)
# Add description
customer_latest_invoice_amount_Z_score_to_invoice_amount_28d.update_description(
"Z-Score of the Latest invoice Amount for the customer compared to customer invoices over a 28d period"
)