Skip to content

featurebyte.Feature.cd.normalize

normalize( ) -> Feature

Description

Normalizes the dictionary values by dividing each value by the sum of all values. If the sum of values is 0 for a row, returns an empty dictionary.

Returns

  • Feature
    A new Feature object with normalized values.

Examples

Create a new feature by normalizing the dictionary values:

>>> counts = catalog.get_feature("CustomerProductGroupCounts_7d")
>>> new_feature = counts.cd.normalize()
>>> new_feature.name = "CustomerProductGroupCountsNormalized_7d"
Preview the features:

>>> features = fb.FeatureGroup([counts, new_feature])
>>> df = features.preview(
...     pd.DataFrame([
...         {
...             "POINT_IN_TIME": "2022-04-15 10:00:00",
...             "GROCERYCUSTOMERGUID": "2f4c1578-29d6-44b7-83da-7c5bfb981fa0",
...         }
...     ])
... )

Dictionary feature:

>>> df["CustomerProductGroupCounts_7d"].iloc[0]
{'Chips et Tortillas': 1, 'Colas, Thés glacés et Sodas': 3, 'Crèmes et Chantilly': 1, 'Pains': 1, 'Œufs': 1}

New feature (values normalized to sum to 1):

>>> df["CustomerProductGroupCountsNormalized_7d"].iloc[0]
{'Chips et Tortillas': 0.143, 'Colas, Thés glacés et Sodas': 0.429, 'Crèmes et Chantilly': 0.143, 'Pains': 0.143, 'Œufs': 0.143}