AWS Machine Learning Blog

Introducing Recommendation Filters in Amazon Personalize

This blog post was last reviewed or updated April, 2022 with updates to how filters are configured.

Today, we are pleased to announce the addition of Recommendation Filters in Amazon Personalize, which improve the relevance of personalized recommendations by filtering out recommendations for products that users have already purchased, videos they have already watched, or other digital content they have already consumed. Receiving such recommendations can be a frustrating experience for users, which can lead to lower engagement, and consequently revenue loss for the business. Customers typically address this today by writing custom code, which compares the recommendations for each user generated by their personalization system such as Amazon Personalize, with the conversion data stored in their database, and removes recommendations for products that the user has already purchased. This can be a time consuming and error prone process for customers. Recommendation Filters in Amazon Personalize eliminate the need for writing this custom code, and automatically filter out recommendations for products that users have already purchased.

Based on over 20 years of personalization experience, Amazon Personalize enables you to improve customer engagement by powering personalized product and content recommendations, and targeted marketing promotions. Using machine learning, Amazon Personalize creates higher-quality recommendations for your websites and applications. You can get started without any prior machine learning experience using simple APIs to easily build sophisticated personalization capabilities in just a few clicks. Amazon Personalize will process and examine your data, identify what is meaningful, automatically pick the right machine learning algorithm, and train and optimize a custom model based on your data. All of your data is encrypted to be private and secure, and is only used to create recommendations for your users.

Setting up and using Recommendation Filters is simple. You first use the Amazon Personalize console or API to create a filter using an Amazon Personalize-specific DSL (Domain Specific Language). You can then apply this filter while querying for real time recommendations using the GetRecommendations or GetPersonalizedRanking API; or while generating recommendations in batch mode through a batch inference job.

This post walks you through the process of setting up and using Recommendation Filters in Amazon Personalize.

Prerequisites

To define and apply filters you will first need to setup the following Personalize resources. To follow along with this post use the Amazon Personalize console and follow the Getting Started (Console) guide to :

  1. Create a dataset group.
  2. Create an Interactions dataset using the schema provided below and then import data using this data file.
    {
        "type": "record",
        "name": "Interactions",
        "namespace": "com.amazonaws.concierge.schema",
        "fields": [
            {
                "name": "ITEM_ID",
                "type": "string"
            },
            {
                "name": "USER_ID",
                "type": "string"
            },
            {
                "name": "TIMESTAMP",
                "type": "long"
            },
            {
                "name": "EVENT_TYPE",
                "type": "string"
            },
            {
                "name": "EVENT_VALUE",
                "type": [
                    "null",
                    "float"
                ]
            }
        ],
        "version": "1.0"
    }
    
  3.  Create a solution using any recipe. In this post we use aws-hrnn recipe.
  4. Create a campaign.

Creating your filter

Now that you have set up your Amazon Personalize resources, you can define and test filters.

Filter expression language

Amazon Personalize uses its own domain-specific language called filter expressions to determine which items to exclude or include in a set of recommendations. Filter expressions are scoped to a dataset group; you can only use them to filter results for campaigns or solution versions (batch recommendations) scoped to the same dataset group. Amazon Personalize can filter items based on a user’s interaction history. The following are some examples of filter expressions.

  • To remove all previously purchased items from user recommendations, use the following filter expression:
    EXCLUDE itemId WHERE INTERACTIONS.event_type in ("Purchase")
  • To remove all items that a user has previously viewed, clicked, or downloaded, use the following filter expression:
    EXCLUDE itemId WHERE INTERACTIONS.event_type in ("View","Click","Download")
  • To remove all items that a user has previously interacted with, use the following filter expression:
    EXCLUDE itemId WHERE INTERACTIONS.event_type in ("*")

You can filter on any event type that is included in your interaction dataset. For more information, see Datasets and Schemas. For further details on filter definition DSL, see our documentation for more details.

Creating a filter through the console

You can use the preceding DSL to create a filter on the Amazon Personalize console.  To create a filter, complete the following steps:

  1. On the Amazon Personalize console, on the Filters tab, choose Create filter.
  2. For Filter name, enter the name for your filter.
  3. For Expression, select Build expression. Alternatively, you can add your expression manually.
  4. To chain additional expressions with your filter, choose +.
  5. To add additional filter expressions, choose Add expression.
  6. Choose Finish.

Creating a filter takes you to a page containing detailed information about your filter. Here you can view more information about your filter, including the filter ARN and the corresponding filter expression you created. You can also delete filters on this page or create more filters from the summary page.

You can also create filters via the createFilter API in Amazon Personalize. For more information, see our documentation.

Applying your filter to real-time recommendations through the console

The Amazon Personalize console allows you to spot-check real-time recommendations from the Campaigns page. From this page you can apply filters while retrieving recommendations for a user. To do so, navigate to the Campaign tab; this should be in the same dataset group that you used to create the filter. You can then test the impact of applying the filter on the recommendations.

Recommendations without a filter

The following screenshot shows recommendations returned with no filter applied.

From the Interactions dataset, you can see that this user has previously purchased some of the items that were recommended. The Interactions dataset contains the following data points:

USER_ID,ITEM_ID,EVENT_TYPE,TIMESTAMP
1,2657,Purchase,964983426
1,2985,Purchase,964983034

The preceding data snippet shows that UserId 1 had already purchased the third and fourth item that was recommended.

Recommendations with a filter

The following screenshot shows results after you remove the previously purchased items from the recommendations by applying the filter we previously defined.

The results show that the filter removed previously purchased items (Item ID 2657 and 2985) from the recommendation; the filter removes items that occur in interactions with an event type Purchase for that user.

Applying your filter to batch recommendations through the console

To apply a filter to batch recommendations through the console, follow the same process as real-time recommendations. On the Create batch inference job page, select the filter name to apply a previously created filter to your batch recommendations.

Applying your filter to real-time recommendations through the SDK

You can also apply filters to recommendations that are served through your SDK or APIs by supplying the FilterArn as an additional and optional parameter to your GetRecommendations calls. Use “filterArn” as the parameter key and supply the filterArn as a string for the value. FilterArn is a unique identifying key that the CreateFilter API call returns. You can also find a filter’s ARN on the filter’s detailed information page.

The following example code is a request body for GetRecommendations API that applies a filter to a recommendation:

{
    "campaignArn": "arn:aws:personalize:us-west-2:000000000000:campaign/test-campaign",
    "userId": "1",
    "itemId": "1",
    "numResults": 5,
    "filterArn": "arn:aws:personalize:us-west-2:000000000000:filter/test-filter"
}

Applying your filter to batch recommendations through the SDK

To apply filters to Batch recommendations when using a SDK you provide the filterArn in the request body as an optional parameter. Use “filterArn” as the key and the filterArn as the value.

Summary

Recommendation Filters in Amazon Personalize allow you to fine-tune recommendations according to your business needs without having to implement post-processing logic on your own. For more information about optimizing your user experience with Amazon Personalize, see What Is Amazon Personalize?


About the author

Adam is a software development engineer at AWS, working as a member of the Amazon Personalize development team. During his off time, Adam enjoys making music and attending music events.

Vaibhav Sethi is the Product Manager for Amazon Personalize. He focuses on delivering products that make it easier to build machine learning solutions. In his spare time, he enjoys hiking and reading.