Getting a personalized ranking (AWS SDKs)
The following code samples show how different variations of how to get a personalized ranking with the AWS SDKs.
Topics
Getting a personalized ranking
The following code shows how to get a personalized ranking for a user. Specify the user's ID and a list of item IDs to be ranked for the user. The item IDs must be in the data that you used to train the solution version. A list of ranked recommendations is returned. Amazon Personalize considers the first item in the list of most interest to the user.
Including item metadata in a personalized ranking
If you enabled metadata in recommendations for your campaign, you can specify the Items dataset metadata columns to include in the response. For information about enabling metadata, see Item metadata in recommendations.
The following code sample shows how to specify the metadata columns as part of your request for a personalized ranking.
import boto3 personalizeRt = boto3.client('personalize-runtime') response = personalizeRt.get_personalized_ranking( campaignArn = "
Campaign arn
", userId = "UserID
", inputList = ['ItemID1
','ItemID2
'], metadataColumns = { "ITEMS": ['columnNameA
','columnNameB
'] } ) print("Personalized Ranking") for item in response['personalizedRanking']: print (item['itemId']) print (item['metadata'])
Getting a personalized ranking using contextual metadata
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'])