Real-Time Recommendations

You can get real-time recommendations from Amazon Personalize with a campaign.

You can use the GetRecommendations or GetPersonalizedRanking API operations to get real-time 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.

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, where “exp” is the exponential function, w̅ u and wi/j are user and item embeddings respectively, and the Greek letter sigma (Σ) represents summation over all items in the item dataset:

\[Image NOT FOUND\]

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

Getting Recommendations using Python

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 using Python

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.

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:

\[Image NOT FOUND\]

Getting a Personalized Ranking using Python

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 using python

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'])