CUSTOMER Time Since Latest Change in State
SDK code to create CUSTOMER_Time_Since_Latest_Change_in_State¶
Feature description:
Time (in days) Since Latest Change in State 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 State¶
In [ ]:
Copied!
# Create change view from GROCERYCUSTOMER table to track changes in State.
grocerycustomer_state_change_view =\
catalog.get_table("GROCERYCUSTOMER").get_change_view(
track_changes_column = "State"
)
# Create change view from GROCERYCUSTOMER table to track changes in State.
grocerycustomer_state_change_view =\
catalog.get_table("GROCERYCUSTOMER").get_change_view(
track_changes_column = "State"
)
In [ ]:
Copied!
# Filter view to remove change records that correspond to first setting of the field.
cond = grocerycustomer_state_change_view.past_State.isnull()
grocerycustomer_state_change_view =\
grocerycustomer_state_change_view[~cond]
# Filter view to remove change records that correspond to first setting of the field.
cond = grocerycustomer_state_change_view.past_State.isnull()
grocerycustomer_state_change_view =\
grocerycustomer_state_change_view[~cond]
In [ ]:
Copied!
# Group grocerycustomer_state_change_view view by customer entity (GroceryCustomerGuid).
grocerycustomer_state_change_view_by_customer =\
grocerycustomer_state_change_view.groupby("GroceryCustomerGuid")
# Group grocerycustomer_state_change_view view by customer entity (GroceryCustomerGuid).
grocerycustomer_state_change_view_by_customer =\
grocerycustomer_state_change_view.groupby("GroceryCustomerGuid")
In [ ]:
Copied!
# Get Latest Change in State for the customer.
customer_latest_change_in_state =\
grocerycustomer_state_change_view_by_customer.aggregate_over(
"new_ValidFrom", method="latest",
feature_names=["customer_Latest_Change_in_State"],
windows=[None]
)["customer_Latest_Change_in_State"]
# Get Latest Change in State for the customer.
customer_latest_change_in_state =\
grocerycustomer_state_change_view_by_customer.aggregate_over(
"new_ValidFrom", method="latest",
feature_names=["customer_Latest_Change_in_State"],
windows=[None]
)["customer_Latest_Change_in_State"]
In [ ]:
Copied!
# Compare customer_Latest_Change_in_State with point-in-time in days
customer_time_since_latest_change_in_state = (
fb.RequestColumn.point_in_time()
- customer_latest_change_in_state
).dt.day
# Give a name to new feature
customer_time_since_latest_change_in_state.name = \
"CUSTOMER_Time_Since_Latest_Change_in_State"
# Compare customer_Latest_Change_in_State with point-in-time in days
customer_time_since_latest_change_in_state = (
fb.RequestColumn.point_in_time()
- customer_latest_change_in_state
).dt.day
# Give a name to new feature
customer_time_since_latest_change_in_state.name = \
"CUSTOMER_Time_Since_Latest_Change_in_State"
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_state.primary_entity
#Check the primary entity of the feature'
customer_time_since_latest_change_in_state.primary_entity
In [ ]:
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 [ ]:
Copied!
#Preview CUSTOMER_Time_Since_Latest_Change_in_State
customer_time_since_latest_change_in_state.preview(
preview_table
)
#Preview CUSTOMER_Time_Since_Latest_Change_in_State
customer_time_since_latest_change_in_state.preview(
preview_table
)
Save feature¶
In [ ]:
Copied!
# Save feature
customer_time_since_latest_change_in_state.save()
# Save feature
customer_time_since_latest_change_in_state.save()
Add description and see feature definition file¶
In [ ]:
Copied!
# Add description
customer_time_since_latest_change_in_state.update_description(
"Time (in days) Since Latest Change in State for the customer."
)
# See feature definition file
customer_time_since_latest_change_in_state.definition
# Add description
customer_time_since_latest_change_in_state.update_description(
"Time (in days) Since Latest Change in State for the customer."
)
# See feature definition file
customer_time_since_latest_change_in_state.definition