ITEM Discount to INVOICE item Discount ratio
SDK code to create ITEM_Discount_to_INVOICE_item_Discount_ratio¶
Feature description:
Ratio of item Discount to the total sum of item Discount among all items with the same invoice as that item.
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 INVOICEITEMS item table.
invoiceitems_view = catalog.get_view("INVOICEITEMS")
# Get view from INVOICEITEMS item table.
invoiceitems_view = catalog.get_view("INVOICEITEMS")
In [ ]:
Copied!
# Create lookup feature from Discount column for item entity.
item_discount =\
invoiceitems_view["Discount"].as_feature("ITEM_Discount")
# Create lookup feature from Discount column for item entity.
item_discount =\
invoiceitems_view["Discount"].as_feature("ITEM_Discount")
Do aggregation by invoice in INVOICEITEMS¶
See SDK reference for features
See SDK reference to groupby a view
See SDK reference to do aggregation by invoice
See list of aggregation operations
See SDK reference to add an aggregation by invoice to the event view
In [ ]:
Copied!
# Group invoiceitems_view by invoice entity (GroceryInvoiceGuid).
invoiceitems_view_by_invoice =\
invoiceitems_view.groupby("GroceryInvoiceGuid")
# Group invoiceitems_view by invoice entity (GroceryInvoiceGuid).
invoiceitems_view_by_invoice =\
invoiceitems_view.groupby("GroceryInvoiceGuid")
In [ ]:
Copied!
# Sum Discount for the invoice.
invoice_sum_of_item_discount =\
invoiceitems_view_by_invoice.aggregate(
"Discount", method=fb.AggFunc.SUM,
feature_name="INVOICE_Sum_of_item_Discount"
)
# Sum Discount for the invoice.
invoice_sum_of_item_discount =\
invoiceitems_view_by_invoice.aggregate(
"Discount", method=fb.AggFunc.SUM,
feature_name="INVOICE_Sum_of_item_Discount"
)
Compare lookup with aggregation¶
In [ ]:
Copied!
# Get the Ratio of item Discount to the total sum of item Discount among all items with the same
# invoice as that item.
item_discount_to_invoice_item_discount_ratio = (
item_discount
/ invoice_sum_of_item_discount
)
# Give a name to new feature
item_discount_to_invoice_item_discount_ratio.name = \
"ITEM_Discount_to_INVOICE_item_Discount_ratio"
# Get the Ratio of item Discount to the total sum of item Discount among all items with the same
# invoice as that item.
item_discount_to_invoice_item_discount_ratio = (
item_discount
/ invoice_sum_of_item_discount
)
# Give a name to new feature
item_discount_to_invoice_item_discount_ratio.name = \
"ITEM_Discount_to_INVOICE_item_Discount_ratio"
Preview feature¶
Read on the feature primary entity concept
Read on the serving entity concept
In [ ]:
Copied!
#Check the primary entity of the feature'
item_discount_to_invoice_item_discount_ratio.primary_entity
#Check the primary entity of the feature'
item_discount_to_invoice_item_discount_ratio.primary_entity
In [ ]:
Copied!
#Get observation table: 'Preview Table with 10 items'
preview_table = catalog.get_observation_table(
"Preview Table with 10 items"
).to_pandas()
#Get observation table: 'Preview Table with 10 items'
preview_table = catalog.get_observation_table(
"Preview Table with 10 items"
).to_pandas()
In [ ]:
Copied!
#Preview ITEM_Discount_to_INVOICE_item_Discount_ratio
item_discount_to_invoice_item_discount_ratio.preview(
preview_table
)
#Preview ITEM_Discount_to_INVOICE_item_Discount_ratio
item_discount_to_invoice_item_discount_ratio.preview(
preview_table
)
Save feature¶
In [ ]:
Copied!
# Save feature
item_discount_to_invoice_item_discount_ratio.save()
# Save feature
item_discount_to_invoice_item_discount_ratio.save()
Add description and see feature definition file¶
In [ ]:
Copied!
# Add description
item_discount_to_invoice_item_discount_ratio.update_description(
"Ratio of item Discount to the total sum of item Discount among all "
"items with the same invoice as that item."
)
# See feature definition file
item_discount_to_invoice_item_discount_ratio.definition
# Add description
item_discount_to_invoice_item_discount_ratio.update_description(
"Ratio of item Discount to the total sum of item Discount among all "
"items with the same invoice as that item."
)
# See feature definition file
item_discount_to_invoice_item_discount_ratio.definition