Skip to content

featurebyte.Feature.cd.cosine_similarity

cosine_similarity(
other: Feature
) -> Feature

Description

Computes the cosine similarity with another Cross Aggregate feature.

Parameters

  • other: Feature
    Another dictionary feature.

Returns

  • Feature
    Another Cross Aggregate feature.

Examples

Create a similarity feature between two dictionary features:

>>> feature_1 = catalog.get_feature("CustomerProductGroupCounts_7d")
>>> feature_2 = catalog.get_feature("CustomerProductGroupCounts_90d")
>>> similarity = feature_1.cd.cosine_similarity(feature_2)
>>> similarity.name = "CustomerProductGroupCounts_7d_90d_similarity"
Preview the features:

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

>>> 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}

Dictionary feature 2:

>>> df["CustomerProductGroupCounts_90d"].iloc[0]
{'Biscuits apéritifs': 1, 'Biscuits': 1, 'Bonbons': 1, 'Chips et Tortillas': 2, 'Colas, Thés glacés et Sodas': 12, 'Confitures': 1, 'Crèmes et Chantilly': 2, 'Céréales': 1, 'Emballages et sacs': 1, 'Fromages': 3, 'Glaces et Sorbets': 1, 'Glaçons': 1, 'Laits': 4, 'Noix': 1, 'Pains': 4, 'Petit-déjeuner': 2, 'Viande Surgelée': 1, 'Œufs': 1}

Similarity feature:

>>> df["CustomerProductGroupCounts_7d_90d_similarity"].iloc[0]
0.8653846153846161