CUSTOMER X PRODUCT product ProductGroup Representation in CUSTOMER item TotalCost 14d
SDK code to create CUSTOMER_X_PRODUCT_product_ProductGroup_Representation_in_CUSTOMER_item_TotalCost_14d¶
Feature description:
The feature assesses the representation of a given product_ProductGroup in a given CUSTOMER's item TotalCost over a 14d period. A value greater than 1 indicates this product_ProductGroup is over-represented for the particular customer. The evaluation is done by comparing:
- the Percentage of a customer's total item TotalCost, that match the ProductGroup of a specific product over a 14d period.
- the Percentage of the total item TotalCost, that match the ProductGroup of a specific product over a 14d period.
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")
Set windows for aggregation¶
In [ ]:
Copied!
windows = ['14d']
windows = ['14d']
Get view from table¶
In [ ]:
Copied!
# Get view from GROCERYPRODUCT dimension table.
groceryproduct_view = catalog.get_view("GROCERYPRODUCT")
# Get view from GROCERYPRODUCT dimension table.
groceryproduct_view = catalog.get_view("GROCERYPRODUCT")
In [ ]:
Copied!
# Get view from INVOICEITEMS item table.
invoiceitems_view = catalog.get_view("INVOICEITEMS")
# Get view from INVOICEITEMS item table.
invoiceitems_view = catalog.get_view("INVOICEITEMS")
Join views¶
In [ ]:
Copied!
# Join GROCERYPRODUCT view to INVOICEITEMS view.
invoiceitems_view = invoiceitems_view.join(groceryproduct_view, rsuffix="")
# Join GROCERYPRODUCT view to INVOICEITEMS view.
invoiceitems_view = invoiceitems_view.join(groceryproduct_view, rsuffix="")
In [ ]:
Copied!
# Create lookup feature from ProductGroup column for product entity.
product_productgroup =\
groceryproduct_view["ProductGroup"].as_feature("PRODUCT_ProductGroup")
# Create lookup feature from ProductGroup column for product entity.
product_productgroup =\
groceryproduct_view["ProductGroup"].as_feature("PRODUCT_ProductGroup")
Do window aggregation from INVOICEITEMS¶
See SDK reference for features
See SDK reference to groupby a view
See SDK reference to do aggregation over time
In [ ]:
Copied!
# Group INVOICEITEMS view by customer entity (GroceryCustomerGuid) across different ProductGroups.
invoiceitems_view_by_customer_across_productgroup =\
invoiceitems_view.groupby(
['GroceryCustomerGuid'], category="ProductGroup"
)
# Group INVOICEITEMS view by customer entity (GroceryCustomerGuid) across different ProductGroups.
invoiceitems_view_by_customer_across_productgroup =\
invoiceitems_view.groupby(
['GroceryCustomerGuid'], category="ProductGroup"
)
In [ ]:
Copied!
# Distribution representing the cumulative TotalCost of item, categorized by their respective
# product's ProductGroup, for the customer over time.
feature_group =\
invoiceitems_view_by_customer_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"CUSTOMER_item_TotalCost_across_product_ProductGroups"
+ "_" + w for w in windows
],
windows=windows
)
# Get CUSTOMER_item_TotalCost_across_product_ProductGroups_14d object from feature group.
customer_item_totalcost_across_product_productgroups_14d =\
feature_group["CUSTOMER_item_TotalCost_across_product_ProductGroups_14d"]
# Distribution representing the cumulative TotalCost of item, categorized by their respective
# product's ProductGroup, for the customer over time.
feature_group =\
invoiceitems_view_by_customer_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"CUSTOMER_item_TotalCost_across_product_ProductGroups"
+ "_" + w for w in windows
],
windows=windows
)
# Get CUSTOMER_item_TotalCost_across_product_ProductGroups_14d object from feature group.
customer_item_totalcost_across_product_productgroups_14d =\
feature_group["CUSTOMER_item_TotalCost_across_product_ProductGroups_14d"]
In [ ]:
Copied!
# Group INVOICEITEMS view across different ProductGroups.
invoiceitems_view_by_overall_across_productgroup =\
invoiceitems_view.groupby(
[], category="ProductGroup"
)
# Group INVOICEITEMS view across different ProductGroups.
invoiceitems_view_by_overall_across_productgroup =\
invoiceitems_view.groupby(
[], category="ProductGroup"
)
In [ ]:
Copied!
# Distribution representing the cumulative TotalCost of item, categorized by their respective
# product's ProductGroup, over time.
feature_group =\
invoiceitems_view_by_overall_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"OVERALL_item_TotalCost_across_product_ProductGroups"
+ "_" + w for w in windows
],
windows=windows
)
# Get OVERALL_item_TotalCost_across_product_ProductGroups_14d object from feature group.
overall_item_totalcost_across_product_productgroups_14d =\
feature_group["OVERALL_item_TotalCost_across_product_ProductGroups_14d"]
# Distribution representing the cumulative TotalCost of item, categorized by their respective
# product's ProductGroup, over time.
feature_group =\
invoiceitems_view_by_overall_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"OVERALL_item_TotalCost_across_product_ProductGroups"
+ "_" + w for w in windows
],
windows=windows
)
# Get OVERALL_item_TotalCost_across_product_ProductGroups_14d object from feature group.
overall_item_totalcost_across_product_productgroups_14d =\
feature_group["OVERALL_item_TotalCost_across_product_ProductGroups_14d"]
Compare lookup with aggregation across categories¶
In [ ]:
Copied!
# Get the Percentage of a customer's total item TotalCost, that match the ProductGroup of a
# specific product over a 14d period.
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d =\
customer_item_totalcost_across_product_productgroups_14d.cd.get_relative_frequency(
product_productgroup
)
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d.fillna(0)
# Give a name to new feature
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d.name = \
"CUSTOMER_X_PRODUCT_product_ProductGroup_Share_to_CUSTOMER_item_TotalCost_14d"
# Get the Percentage of a customer's total item TotalCost, that match the ProductGroup of a
# specific product over a 14d period.
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d =\
customer_item_totalcost_across_product_productgroups_14d.cd.get_relative_frequency(
product_productgroup
)
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d.fillna(0)
# Give a name to new feature
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d.name = \
"CUSTOMER_X_PRODUCT_product_ProductGroup_Share_to_CUSTOMER_item_TotalCost_14d"
In [ ]:
Copied!
# Get the Percentage of the total item TotalCost, that match the ProductGroup of a specific product
# over a 14d period.
product_productgroup_share_to_overall_item_totalcost_14d =\
overall_item_totalcost_across_product_productgroups_14d.cd.get_relative_frequency(
product_productgroup
)
product_productgroup_share_to_overall_item_totalcost_14d.fillna(0)
# Give a name to new feature
product_productgroup_share_to_overall_item_totalcost_14d.name = \
"PRODUCT_ProductGroup_Share_to_OVERALL_item_TotalCost_14d"
# Get the Percentage of the total item TotalCost, that match the ProductGroup of a specific product
# over a 14d period.
product_productgroup_share_to_overall_item_totalcost_14d =\
overall_item_totalcost_across_product_productgroups_14d.cd.get_relative_frequency(
product_productgroup
)
product_productgroup_share_to_overall_item_totalcost_14d.fillna(0)
# Give a name to new feature
product_productgroup_share_to_overall_item_totalcost_14d.name = \
"PRODUCT_ProductGroup_Share_to_OVERALL_item_TotalCost_14d"
Get Representation of Entity attributes¶
In [ ]:
Copied!
# Get the representation of a given product_ProductGroup in a given CUSTOMER's item TotalCost
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d = (
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d
/ product_productgroup_share_to_overall_item_totalcost_14d
)
# Give a name to new feature
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.name = \
"CUSTOMER_X_PRODUCT_product_ProductGroup_Representation_in_CUSTOMER_item_TotalCost_14d"
# Get the representation of a given product_ProductGroup in a given CUSTOMER's item TotalCost
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d = (
customer_x_product_product_productgroup_share_to_customer_item_totalcost_14d
/ product_productgroup_share_to_overall_item_totalcost_14d
)
# Give a name to new feature
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.name = \
"CUSTOMER_X_PRODUCT_product_ProductGroup_Representation_in_CUSTOMER_item_TotalCost_14d"
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_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.primary_entity
#Check the primary entity of the feature'
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.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_X_PRODUCT_product_ProductGroup_Representation_in_CUSTOMER_item_TotalCost_14d
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.preview(
preview_table
)
#Preview CUSTOMER_X_PRODUCT_product_ProductGroup_Representation_in_CUSTOMER_item_TotalCost_14d
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.preview(
preview_table
)
Save feature¶
In [ ]:
Copied!
# Save feature
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.save()
# Save feature
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.save()
Add description and see feature definition file¶
In [ ]:
Copied!
# Add description
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.update_description(
"The feature assesses the representation of a given "
"product_ProductGroup in a given CUSTOMER's item TotalCost over a 14d "
"period. A value greater than 1 indicates this product_ProductGroup is "
"over-represented for the particular customer. The evaluation is done "
"by comparing: - the Percentage of a customer's total item TotalCost,"
" that match the ProductGroup of a specific product over a 14d period."
" - the Percentage of the total item TotalCost, that match the "
"ProductGroup of a specific product over a 14d period."
)
# See feature definition file
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.definition
# Add description
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.update_description(
"The feature assesses the representation of a given "
"product_ProductGroup in a given CUSTOMER's item TotalCost over a 14d "
"period. A value greater than 1 indicates this product_ProductGroup is "
"over-represented for the particular customer. The evaluation is done "
"by comparing: - the Percentage of a customer's total item TotalCost,"
" that match the ProductGroup of a specific product over a 14d period."
" - the Percentage of the total item TotalCost, that match the "
"ProductGroup of a specific product over a 14d period."
)
# See feature definition file
customer_x_product_product_productgroup_representation_in_customer_item_totalcost_14d.definition