CUSTOMER Time Since Latest Change in PostalCode
SDK code to create CUSTOMER_Time_Since_Latest_Change_in_PostalCode¶
Feature description:
Time (in days) Since Latest Change in PostalCode for the customer.
In [ ]:
Copied!
import featurebyte as fb
fb.use_profile("tutorial")
import featurebyte as fb
fb.use_profile("tutorial")
Activate catalog¶
In [ ]:
Copied!
catalog = fb.Catalog.activate("Grocery Dataset Tutorial")
catalog = fb.Catalog.activate("Grocery Dataset Tutorial")
Get view from table¶
In [ ]:
Copied!
# Get view from GROCERYCUSTOMER scd table.
grocerycustomer_view = catalog.get_view("GROCERYCUSTOMER")
# Get view from GROCERYCUSTOMER scd table.
grocerycustomer_view = catalog.get_view("GROCERYCUSTOMER")
Create features on changes in GROCERYCUSTOMER PostalCode¶
In [ ]:
Copied!
# Create change view from GROCERYCUSTOMER table to track changes in PostalCode.
grocerycustomer_postalcode_change_view =\
catalog.get_table("GROCERYCUSTOMER").get_change_view(
track_changes_column = "PostalCode"
)
# Create change view from GROCERYCUSTOMER table to track changes in PostalCode.
grocerycustomer_postalcode_change_view =\
catalog.get_table("GROCERYCUSTOMER").get_change_view(
track_changes_column = "PostalCode"
)
In [ ]:
Copied!
# Filter view to remove change records that correspond to first setting of the field.
cond = grocerycustomer_postalcode_change_view.past_PostalCode.isnull()
grocerycustomer_postalcode_change_view =\
grocerycustomer_postalcode_change_view[~cond]
# Filter view to remove change records that correspond to first setting of the field.
cond = grocerycustomer_postalcode_change_view.past_PostalCode.isnull()
grocerycustomer_postalcode_change_view =\
grocerycustomer_postalcode_change_view[~cond]
In [ ]:
Copied!
# Group grocerycustomer_postalcode_change_view view by customer entity (GroceryCustomerGuid).
grocerycustomer_postalcode_change_view_by_customer =\
grocerycustomer_postalcode_change_view.groupby("GroceryCustomerGuid")
# Group grocerycustomer_postalcode_change_view view by customer entity (GroceryCustomerGuid).
grocerycustomer_postalcode_change_view_by_customer =\
grocerycustomer_postalcode_change_view.groupby("GroceryCustomerGuid")
In [ ]:
Copied!
# Get Latest Change in PostalCode for the customer.
customer_latest_change_in_postalcode =\
grocerycustomer_postalcode_change_view_by_customer.aggregate_over(
"new_ValidFrom", method="latest",
feature_names=["customer_Latest_Change_in_PostalCode"],
windows=[None]
)["customer_Latest_Change_in_PostalCode"]
# Get Latest Change in PostalCode for the customer.
customer_latest_change_in_postalcode =\
grocerycustomer_postalcode_change_view_by_customer.aggregate_over(
"new_ValidFrom", method="latest",
feature_names=["customer_Latest_Change_in_PostalCode"],
windows=[None]
)["customer_Latest_Change_in_PostalCode"]
In [ ]:
Copied!
# Compare customer_Latest_Change_in_PostalCode with point-in-time in days
customer_time_since_latest_change_in_postalcode = (
fb.RequestColumn.point_in_time()
- customer_latest_change_in_postalcode
).dt.day
# Give a name to new feature
customer_time_since_latest_change_in_postalcode.name = \
"CUSTOMER_Time_Since_Latest_Change_in_PostalCode"
# Compare customer_Latest_Change_in_PostalCode with point-in-time in days
customer_time_since_latest_change_in_postalcode = (
fb.RequestColumn.point_in_time()
- customer_latest_change_in_postalcode
).dt.day
# Give a name to new feature
customer_time_since_latest_change_in_postalcode.name = \
"CUSTOMER_Time_Since_Latest_Change_in_PostalCode"
Preview feature¶
Read on the feature primary entity concept
Read on the serving entity concept
In [ ]:
Copied!
#Check the primary entity of the feature'
customer_time_since_latest_change_in_postalcode.primary_entity
#Check the primary entity of the feature'
customer_time_since_latest_change_in_postalcode.primary_entity
In [ ]:
Copied!
#Get observation table: 'Preview Table with 10 Customers'
preview_table = catalog.get_observation_table(
"Preview Table with 10 Customers"
).to_pandas()
#Get observation table: 'Preview Table with 10 Customers'
preview_table = catalog.get_observation_table(
"Preview Table with 10 Customers"
).to_pandas()
In [ ]:
Copied!
#Preview CUSTOMER_Time_Since_Latest_Change_in_PostalCode
customer_time_since_latest_change_in_postalcode.preview(
preview_table
)
#Preview CUSTOMER_Time_Since_Latest_Change_in_PostalCode
customer_time_since_latest_change_in_postalcode.preview(
preview_table
)
Save feature¶
In [ ]:
Copied!
# Save feature
customer_time_since_latest_change_in_postalcode.save()
# Save feature
customer_time_since_latest_change_in_postalcode.save()
Add description and see feature definition file¶
In [ ]:
Copied!
# Add description
customer_time_since_latest_change_in_postalcode.update_description(
"Time (in days) Since Latest Change in PostalCode for the customer."
)
# See feature definition file
customer_time_since_latest_change_in_postalcode.definition
# Add description
customer_time_since_latest_change_in_postalcode.update_description(
"Time (in days) Since Latest Change in PostalCode for the customer."
)
# See feature definition file
customer_time_since_latest_change_in_postalcode.definition