11. Derive similarity features from bucketing
Create similarity features¶
In this tutorial we will look into another way of deriving features from other features - creating similarity features.
We will create a feature that compares the customer purchase patterns across product groups to all customers purchase patterns. We will first do bucketing at the customer level and at the overall level. Then we will compare the 2 dictionaries with the cosine similarity.
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)
15:31:53 | INFO | SDK version: 1.0.2.dev46 15:31:53 | INFO | No catalog activated. 15:31:53 | INFO | Using profile: tutorial 15:31:53 | INFO | Using configuration file at: /Users/gxav/.featurebyte/config.yaml 15:31:53 | INFO | Active profile: tutorial (https://tutorials.featurebyte.com/api/v1) 15:31:53 | INFO | SDK version: 1.0.2.dev46 15:31:53 | INFO | No catalog activated. 15:31:53 | INFO | Catalog activated: Grocery Dataset Tutorial
In [2]:
Copied!
# Get view from GROCERYPRODUCT dimension table.
groceryproduct_view = catalog.get_view("GROCERYPRODUCT")
# Get view from INVOICEITEMS item table.
invoiceitems_view = catalog.get_view("INVOICEITEMS")
# Get view from GROCERYPRODUCT dimension table.
groceryproduct_view = catalog.get_view("GROCERYPRODUCT")
# Get view from INVOICEITEMS item table.
invoiceitems_view = catalog.get_view("INVOICEITEMS")
Join views¶
In [3]:
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="")
Create distribution features across |product groups¶
In [4]:
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 [5]:
Copied!
# Create Buckets representing the cumulative TotalCost of item, categorized by ProductGroup,
# for the customer over the past 26 weeks.
# The result is presented as a dictionary where the ProductGroup serves as the key
# and its corresponding sum of item TotalCost for the customer forms the value.
feature_group =\
invoiceitems_view_by_customer_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"CUSTOMER_item_TotalCost_across_product_ProductGroups_26w"
],
windows=["26w"]
)
# Get CUSTOMER_item_TotalCost_across_product_ProductGroups_26w object from feature group.
customer_item_totalcost_across_product_productgroups_26w =\
feature_group["CUSTOMER_item_TotalCost_across_product_ProductGroups_26w"]
# Create Buckets representing the cumulative TotalCost of item, categorized by ProductGroup,
# for the customer over the past 26 weeks.
# The result is presented as a dictionary where the ProductGroup serves as the key
# and its corresponding sum of item TotalCost for the customer forms the value.
feature_group =\
invoiceitems_view_by_customer_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"CUSTOMER_item_TotalCost_across_product_ProductGroups_26w"
],
windows=["26w"]
)
# Get CUSTOMER_item_TotalCost_across_product_ProductGroups_26w object from feature group.
customer_item_totalcost_across_product_productgroups_26w =\
feature_group["CUSTOMER_item_TotalCost_across_product_ProductGroups_26w"]
In [6]:
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 [7]:
Copied!
# Create Buckets representing the cumulative TotalCost of item, categorized by ProductGroup,
# for ALL customers over the past 26 weeks.
# The result is presented as a dictionary where the ProductGroup serves as the key
# and its corresponding sum of item TotalCost forms the value.
feature_group =\
invoiceitems_view_by_overall_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"OVERALL_item_TotalCost_across_product_ProductGroups_26w"
],
windows=["26w"]
)
# Get OVERALL_item_TotalCost_across_product_ProductGroups_26w object from feature group.
overall_item_totalcost_across_product_productgroups_26w =\
feature_group["OVERALL_item_TotalCost_across_product_ProductGroups_26w"]
# Create Buckets representing the cumulative TotalCost of item, categorized by ProductGroup,
# for ALL customers over the past 26 weeks.
# The result is presented as a dictionary where the ProductGroup serves as the key
# and its corresponding sum of item TotalCost forms the value.
feature_group =\
invoiceitems_view_by_overall_across_productgroup.aggregate_over(
"TotalCost", method=fb.AggFunc.SUM,
feature_names=[
"OVERALL_item_TotalCost_across_product_ProductGroups_26w"
],
windows=["26w"]
)
# Get OVERALL_item_TotalCost_across_product_ProductGroups_26w object from feature group.
overall_item_totalcost_across_product_productgroups_26w =\
feature_group["OVERALL_item_TotalCost_across_product_ProductGroups_26w"]
Derive Similarity feature across entities¶
In [8]:
Copied!
# Derive Similarity feature from cosine similarity between
# CUSTOMER_item_TotalCost_across_product_ProductGroups_26w
# and OVERALL_item_TotalCost_across_product_ProductGroups_26w
customer_vs_overall_item_totalcost_across_product_productgroups_26w =\
customer_item_totalcost_across_product_productgroups_26w.cd.cosine_similarity(
overall_item_totalcost_across_product_productgroups_26w
)
# Give a name to new feature
customer_vs_overall_item_totalcost_across_product_productgroups_26w.name = \
"CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w"
# Derive Similarity feature from cosine similarity between
# CUSTOMER_item_TotalCost_across_product_ProductGroups_26w
# and OVERALL_item_TotalCost_across_product_ProductGroups_26w
customer_vs_overall_item_totalcost_across_product_productgroups_26w =\
customer_item_totalcost_across_product_productgroups_26w.cd.cosine_similarity(
overall_item_totalcost_across_product_productgroups_26w
)
# Give a name to new feature
customer_vs_overall_item_totalcost_across_product_productgroups_26w.name = \
"CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w"
Preview feature¶
In [9]:
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 [10]:
Copied!
# Preview CUSTOMER_item_TotalCost_across_product_ProductGroups_26w
customer_item_totalcost_across_product_productgroups_26w.preview(
preview_table
)
# Preview CUSTOMER_item_TotalCost_across_product_ProductGroups_26w
customer_item_totalcost_across_product_productgroups_26w.preview(
preview_table
)
Out[10]:
POINT_IN_TIME | GROCERYINVOICEITEMGUID | CUSTOMER_item_TotalCost_across_product_ProductGroups_26w | |
---|---|---|---|
0 | 2022-11-09 18:07:20 | 37122b82-478b-4d9e-b236-69629b592c0b | {'Aide à la Pâtisserie': 1.49, 'Autres Produit... |
1 | 2022-10-05 14:30:03 | fc4769cf-41d6-4fb0-9cfc-fe30502cfa18 | {'Adoucissants et Soin du linge': 2.0, 'Aide à... |
2 | 2023-04-19 15:25:05 | 8a60f455-aff1-4b4e-8c63-9ab89df2715d | {'Aide à la Pâtisserie': 8.35, 'Biscuits apéri... |
3 | 2022-08-16 15:37:21 | 6781acc6-652d-4867-a138-2d8adb278886 | {'Autres Produits Laitiers': 12.76, 'Bières et... |
4 | 2023-03-13 21:11:43 | cfc620a2-0054-4b4b-99d5-040cf87cfe2d | {'Adoucissants et Soin du linge': 8.78, 'Aide ... |
5 | 2023-03-19 15:22:18 | dcdb7c98-dd62-4287-b432-bfe3a2317ebc | {'Adoucissants et Soin du linge': 4.99, 'Aide ... |
6 | 2023-02-27 13:27:47 | b807e05c-ff1c-4fb3-a760-e0e8ce29c859 | {'Aide à la Pâtisserie': 29.16, 'Autres Produi... |
7 | 2023-02-15 21:10:22 | 5daf8edb-8625-4653-aaa0-9ac03df92017 | {'Adoucissants et Soin du linge': 1.0, 'Aide à... |
8 | 2022-11-03 14:39:00 | 9faf5936-d4bb-4709-a530-c1624ec003a5 | {'Adoucissants et Soin du linge': 26.96, 'Aide... |
9 | 2022-11-18 10:07:49 | ae2ccf38-4e5d-4c76-b1e5-04f12307e45b | {'Adoucissants et Soin du linge': 11.98, 'Aide... |
In [11]:
Copied!
# Preview OVERALL_item_TotalCost_across_product_ProductGroups_26w
overall_item_totalcost_across_product_productgroups_26w.preview(
preview_table
)
# Preview OVERALL_item_TotalCost_across_product_ProductGroups_26w
overall_item_totalcost_across_product_productgroups_26w.preview(
preview_table
)
Out[11]:
POINT_IN_TIME | GROCERYINVOICEITEMGUID | OVERALL_item_TotalCost_across_product_ProductGroups_26w | |
---|---|---|---|
0 | 2022-11-09 18:07:20 | 37122b82-478b-4d9e-b236-69629b592c0b | {'Adoucissants et Soin du linge': 1141.88, 'Ai... |
1 | 2022-10-05 14:30:03 | fc4769cf-41d6-4fb0-9cfc-fe30502cfa18 | {'Adoucissants et Soin du linge': 1088.58, 'Ai... |
2 | 2023-04-19 15:25:05 | 8a60f455-aff1-4b4e-8c63-9ab89df2715d | {'Adoucissants et Soin du linge': 1202.6, 'Aid... |
3 | 2023-03-13 21:11:43 | cfc620a2-0054-4b4b-99d5-040cf87cfe2d | {'Adoucissants et Soin du linge': 1213.52, 'Ai... |
4 | 2023-02-15 21:10:22 | 5daf8edb-8625-4653-aaa0-9ac03df92017 | {'Adoucissants et Soin du linge': 1170.86, 'Ai... |
5 | 2022-08-16 15:37:21 | 6781acc6-652d-4867-a138-2d8adb278886 | {'Adoucissants et Soin du linge': 1119.67, 'Ai... |
6 | 2022-11-18 10:07:49 | ae2ccf38-4e5d-4c76-b1e5-04f12307e45b | {'Adoucissants et Soin du linge': 1187.5, 'Aid... |
7 | 2022-11-03 14:39:00 | 9faf5936-d4bb-4709-a530-c1624ec003a5 | {'Adoucissants et Soin du linge': 1125.9, 'Aid... |
8 | 2023-02-27 13:27:47 | b807e05c-ff1c-4fb3-a760-e0e8ce29c859 | {'Adoucissants et Soin du linge': 1201.79, 'Ai... |
9 | 2023-03-19 15:22:18 | dcdb7c98-dd62-4287-b432-bfe3a2317ebc | {'Adoucissants et Soin du linge': 1220.18, 'Ai... |
In [12]:
Copied!
# Preview CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w
customer_vs_overall_item_totalcost_across_product_productgroups_26w.preview(
preview_table
)
# Preview CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w
customer_vs_overall_item_totalcost_across_product_productgroups_26w.preview(
preview_table
)
Out[12]:
POINT_IN_TIME | GROCERYINVOICEITEMGUID | CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w | |
---|---|---|---|
0 | 2022-11-09 18:07:20 | 37122b82-478b-4d9e-b236-69629b592c0b | 0.805024 |
1 | 2022-10-05 14:30:03 | fc4769cf-41d6-4fb0-9cfc-fe30502cfa18 | 0.741273 |
2 | 2023-04-19 15:25:05 | 8a60f455-aff1-4b4e-8c63-9ab89df2715d | 0.754857 |
3 | 2022-08-16 15:37:21 | 6781acc6-652d-4867-a138-2d8adb278886 | 0.722791 |
4 | 2023-03-13 21:11:43 | cfc620a2-0054-4b4b-99d5-040cf87cfe2d | 0.544831 |
5 | 2023-03-19 15:22:18 | dcdb7c98-dd62-4287-b432-bfe3a2317ebc | 0.855608 |
6 | 2023-02-27 13:27:47 | b807e05c-ff1c-4fb3-a760-e0e8ce29c859 | 0.760363 |
7 | 2023-02-15 21:10:22 | 5daf8edb-8625-4653-aaa0-9ac03df92017 | 0.614021 |
8 | 2022-11-03 14:39:00 | 9faf5936-d4bb-4709-a530-c1624ec003a5 | 0.677794 |
9 | 2022-11-18 10:07:49 | ae2ccf38-4e5d-4c76-b1e5-04f12307e45b | 0.731057 |
Save feature¶
In [13]:
Copied!
# Save feature
customer_vs_overall_item_totalcost_across_product_productgroups_26w.save()
# Save feature
customer_vs_overall_item_totalcost_across_product_productgroups_26w.save()
Done! |████████████████████████████████████████| 100% in 6.1s (0.17%/s)
As always, add description and view definition file¶
In [14]:
Copied!
# Add description
customer_vs_overall_item_totalcost_across_product_productgroups_26w.update_description(
"Similarity between the customer and all customers measured by the "
"Cosine Similarity between the Distribution representing the cumulative"
" TotalCost of item, categorized by their respective product's "
"ProductGroup, over 26w for both entities."
)
# See feature definition file
customer_vs_overall_item_totalcost_across_product_productgroups_26w.definition
# Add description
customer_vs_overall_item_totalcost_across_product_productgroups_26w.update_description(
"Similarity between the customer and all customers measured by the "
"Cosine Similarity between the Distribution representing the cumulative"
" TotalCost of item, categorized by their respective product's "
"ProductGroup, over 26w for both entities."
)
# See feature definition file
customer_vs_overall_item_totalcost_across_product_productgroups_26w.definition
Out[14]:
# Generated by SDK version: 1.0.2.dev46
from bson import ObjectId
from featurebyte import DimensionTable
from featurebyte import FeatureJobSetting
from featurebyte import ItemTable
# item_table name: "INVOICEITEMS", event_table name: "GROCERYINVOICE"
item_table = ItemTable.get_by_id(ObjectId("662b5778aa13c89fa14554e2"))
item_view = item_table.get_view(
event_suffix=None,
view_mode="manual",
drop_column_names=["record_available_at"],
column_cleaning_operations=[],
event_drop_column_names=["record_available_at"],
event_column_cleaning_operations=[],
event_join_column_names=[
"Timestamp",
"GroceryInvoiceGuid",
"GroceryCustomerGuid",
"tz_offset",
],
)
# dimension_table name: "GROCERYPRODUCT"
dimension_table = DimensionTable.get_by_id(ObjectId("662b577aaa13c89fa14554e3"))
dimension_view = dimension_table.get_view(
view_mode="manual", drop_column_names=[], column_cleaning_operations=[]
)
joined_view = item_view.join(
dimension_view, on="GroceryProductGuid", how="left", rsuffix="", rprefix=""
)
grouped = joined_view.groupby(
by_keys=["GroceryCustomerGuid"], category="ProductGroup"
).aggregate_over(
value_column="TotalCost",
method="sum",
windows=["26w"],
feature_names=["CUSTOMER_item_TotalCost_across_product_ProductGroups_26w"],
feature_job_setting=FeatureJobSetting(
blind_spot="120s", frequency="3600s", time_modulo_frequency="120s"
),
skip_fill_na=True,
)
feat = grouped["CUSTOMER_item_TotalCost_across_product_ProductGroups_26w"]
grouped_1 = joined_view.groupby(
by_keys=[], category="ProductGroup"
).aggregate_over(
value_column="TotalCost",
method="sum",
windows=["26w"],
feature_names=["OVERALL_item_TotalCost_across_product_ProductGroups_26w"],
feature_job_setting=FeatureJobSetting(
blind_spot="120s", frequency="3600s", time_modulo_frequency="120s"
),
skip_fill_na=True,
)
feat_1 = grouped_1["OVERALL_item_TotalCost_across_product_ProductGroups_26w"]
feat_2 = feat.cd.cosine_similarity(other=feat_1)
feat_2.name = (
"CUSTOMER_vs_OVERALL_item_TotalCost_across_product_ProductGroups_26w"
)
output = feat_2
output.save(_id=ObjectId("662b586bf010e567696bfd3b"))
In [ ]:
Copied!