Getting real-time recommendations - Amazon Personalize

Getting real-time recommendations

You can get real-time recommendations from Amazon Personalize with a campaign. For example, suppose you have a campaign that is designed to give movie recommendations. You can use the GetRecommendations or GetPersonalizedRanking API operations to get real-time movie recommendations for users signed into your application or website. You can also get recommendations using Amazon Personalize console, where the top recommendations for the user appear in a table on the details page for the campaign.

Increasing recommendation relevance with contextual metadata

To increase recommendation relevance, include contextual metadata for a user, such as their device type or the time of day, when you get recommendations or get a personalized ranking.

To use contextual metadata, you must meet the following requirements:

  • The schema of the Interactions dataset must have contextual metadata fields (see Datasets and schemas).

  • The solution version backing the campaign must use a recipe of type USER_PERSONALIZATION or RELATED_ITEMS (see Step 1: Choosing a recipe).

For more information about the benefits of including contextual metadata, see Contextual metadata.

For examples that show how to include contextual metadata using the AWS SDK for Python see Getting recommendations using contextual metadata (AWS Python SDK) and Getting a personalized ranking using contextual metadata (AWS Python SDK).

Getting recommendations

To get recommendations, call the GetRecommendations API operation, or get recommendations for a user on the campaign details page in the console. For an example using the AWS CLI, see Step 4: Get recommendations.

How scoring works

To make recommendations, Amazon Personalize generates scores for the items in your Items dataset based on a user's interaction data and metadata. These scores represent the relative certainty that Amazon Personalize has in which item the user will select next. Higher scores represent greater certainty.

Models that are based on USER_PERSONALIZATION recipes score all of the items in your Items dataset relative to each other on a scale from 0 to 1 (both inclusive), so that the total of all scores equals 1. For example, if you're getting movie recommendations for a user and there are three movies in the Items dataset, their scores might be 0.6, 0.3, and 0.1. Similarly, if you have 1,000 movies in your inventory, the highest-scoring movies might have very small scores (the average score would be.001), but, because scoring is relative, the recommendations are still valid.

In mathematical terms, scores for each user-item pair (u,i) are computed according to the following formula, u and wi/j are user and item embeddings respectively, and the Greek letter sigma (Σ) represents summation over all items in the item dataset:

Note

Amazon Personalize doesn't show scores for SIMS or Popularity-Count-based models.

Getting recommendations (console)

To get recommendations for a user in the Amazon Personalize console, choose the campaign that you are using and then provide their User ID, optionally choose a filter, and optionally provide any context data.

To get recommendations for a user

  1. Open the Amazon Personalize console at https://console.aws.amazon.com/personalize/home and sign into your account.

  2. Choose the dataset group that contains the campaign you are using.

  3. In the navigation pane, choose Campaigns.

  4. On the Campaigns page, choose the target campaign.

  5. Under Test campaign results, enter the User ID of the user that you want to get recommendations for.

  6. Optionally choose a filter. For more information, see Filtering recommendations.

  7. If your campaign uses contextual metadata (for requirements see Increasing recommendation relevance with contextual metadata) optionally provide context data.

    For each context, for the Key, enter the metadata field, and for the Value, enter the context data.

  8. Choose Get recommendations. A table containing the user’s top recommendations appears.

Getting recommendations (AWS Python SDK)

Use the following code to get a recommendation. Change the value of userId to a user ID that is in the data that you used to train the solution. A list of recommended items for the user is displayed.

import boto3 personalizeRt = boto3.client('personalize-runtime') response = personalizeRt.get_recommendations( campaignArn = 'Campaign ARN', userId = 'User ID') print("Recommended items") for item in response['itemList']: print (item['itemId'])

Getting recommendations using contextual metadata (AWS Python SDK)

Use the following code to get a recommendation based on contextual metadata. For context, for each key-value pair, provide the metadata field as the key and the context data as the value. In the following sample code, the key is DEVICE and the value is mobile phone. Replace these values and the Campaign ARN and User ID with your own. A list of recommended items for the user is displayed.

import boto3 personalizeRt = boto3.client('personalize-runtime') response = personalizeRt.get_recommendations( campaignArn = 'Campaign ARN', userId = 'User ID', context = { 'DEVICE': 'mobile phone' } ) print("Recommended items") for item in response['itemList']: print (item['itemId'])

Getting a personalized ranking

A personalized ranking is a list of recommended items that are re-ranked for a specific user. To get personalized rankings, call the GetPersonalizedRanking API operation or get recommendations from a campaign in the console.

Note

The solution backing the campaign must have been created using a recipe of type PERSONALIZED_RANKING. For more information, see Step 1: Choosing a recipe.

How scoring works

Like the scores returned by the GetRecommendations operation, GetPersonalizedRanking scores sum to 1, but because the list of considered items is much smaller than your full Items dataset, recommendation scores tend to be higher.

Mathematically, the scoring function for GetPersonalizedRanking is identical to GetRecommendations, except that it only considers the input items. This means that scores closer to 1 become more likely, as there are fewer other choices to divide up the score:

Getting a personalized ranking (console)

To get a personalized ranking for a user from the Amazon Personalize console, choose the campaign that you are using and then provide their user ID, specify the list of items you want ranked for the user, optionally choose a filter, and optionally provide any context data.

To get a personalized ranking for a user

  1. Open the Amazon Personalize console at https://console.aws.amazon.com/personalize/ and sign into your account.

  2. Choose the dataset group that contains the campaign you are using.

  3. In the navigation pane, choose Campaigns.

  4. On the Campaigns page, choose the target campaign.

  5. Under Test campaign results, enter the User ID of the user that you want to get recommendations for.

  6. For Item IDs, enter the list of items to be ranked for the user.

  7. Optionally choose a filter. For more information, see Filtering recommendations.

  8. If your campaign uses contextual metadata (for requirements see Increasing recommendation relevance with contextual metadata) optionally provide context data.

    For each context, for the Key, enter the metadata field, and for the Value, enter the context data.

  9. Choose Get personalized item rankings. A table containing the items ranked in order of predicted interest for the user appears.

Getting a personalized ranking (AWS Python SDK)

Use the following code to get a personalized ranking. Change the value of userId and inputList to a user ID and list of item IDs that are in the data that you used to train the solution. A list of ranked recommendations is displayed. Amazon Personalize considers the first item in the list of most interest to the user.

import boto3 personalizeRt = boto3.client('personalize-runtime') response = personalizeRt.get_personalized_ranking( campaignArn = "Campaign arn", userId = "UserID", inputList = ['ItemID1','ItemID2']) print("Personalized Ranking") for item in response['personalizedRanking']: print (item['itemId'])

Getting a personalized ranking using contextual metadata (AWS Python SDK)

Use the following code to get a personalized ranking based on contextual metadata. For context, for each key-value pair, provide the metadata field as the key and the context data as the value. In the following sample code, the key is DEVICE and the value is mobile phone. Replace these values and the Campaign ARN and User ID with your own. Also change inputList to a list of item IDs that are in the data that you used to train the solution. Amazon Personalize considers the first item in the list of most interest to the user.

import boto3 personalizeRt = boto3.client('personalize-runtime') response = personalizeRt.get_personalized_ranking( campaignArn = "Campaign ARN", userId = "User ID", inputList = ['ItemID1', 'ItemID2'], context = { 'DEVICE': 'mobile phone' } ) print("Personalized Ranking") for item in response['personalizedRanking']: print(item['itemId'])

Personalized-Ranking sample notebook

For a sample Jupyter notebook that shows how to use the Personalized-Ranking recipe see Personalize Ranking Example.