A campaign is used to make recommendations for your users. In this module, you create a campaign by deploying your solution version. To deploy a solution version, you create a campaign in the console or by calling the CreateCampaign API. You choose which version of the solution to use.

In the following steps, you create the campaign and wait for the campaign status to be active. Then, you use your campaign to get real-time movie title recommendations for users from Amazon Personalize.

Time to Complete Module: 20 Minutes


  • Step 1. Create your campaign

    In this step, you create a campaign for the solution version you trained using the User-Personalization recipe. This recipe includes the optional explorationWeight and explorationItemAgeCutOff for itemExplorationConfig. For more information, see User-Personalization.
     
    Run the following code block to create your campaign:
    create_campaign_response = personalize.create_campaign(
        name = "personalize-demo-camp",
        solutionVersionArn = solution_version_arn,
        minProvisionedTPS = 1,
        campaignConfig = {
            "itemExplorationConfig": {
                "explorationWeight": "0.3",
    	"explorationItemAgeCutOff": "30"
            }
        }
    )
    
    campaign_arn = create_campaign_response['campaignArn']
    print(json.dumps(create_campaign_response, indent=2))
    
    max_time = time.time() + 3*60*60 # 3 hours
    while time.time() < max_time:
        describe_campaign_response = personalize.describe_campaign(
            campaignArn = campaign_arn
        )
        status = describe_campaign_response["campaign"]["status"]
        print("Campaign: {}".format(status))
        
        if status == "ACTIVE" or status == "CREATE FAILED":
            break
            
        time.sleep(60)

    Wait for the Campaign status to display ACTIVE. This step can take 10-15 minutes.

    tutorial-personalize-create-campaign

    (Click to enlarge)

    tutorial-personalize-create-campaign
  • Step 2. Get real-time recommendations

    In this step, you use a sample, random user_id and get the recommendation for that user_id. Note you may see a different result because this recommendation is for a random user.
     
    Run the following code to get a recommendation for a user:
    # Build a map to convert a movie id to the movie title
    movies = pd.read_csv(dataset_dir + '/movies.csv', usecols=[0,1])
    movies['movieId'] = movies['movieId'].astype(str)
    movie_map = dict(movies.values)
    
    # Getting a random user:
    user_id, item_id = interactions_df[['USER_ID', 'ITEM_ID']].sample().values[0]
    
    get_recommendations_response = personalize_runtime.get_recommendations(
        campaignArn = campaign_arn,
        userId = str(user_id),
    )
    # Update DF rendering
    pd.set_option('display.max_rows', 30)
    
    print("Recommendations for user: ", user_id)
    
    item_list = get_recommendations_response['itemList']
    
    recommendation_list = []
    
    for item in item_list:
        title = movie_map[item['itemId']]
        recommendation_list.append(title)
        
    recommendations_df = pd.DataFrame(recommendation_list, columns = ['OriginalRecs'])
    recommendations_df.head()

    In this example, you can see the top movie title recommendations for this user. These are the movies that the model thinks the user will like based on the users' interaction data.

    Great! You’ve generated real-time movie title recommendations for a user.

    tutorial-personalize-recommendation-output

    (Click to enlarge)

    tutorial-personalize-recommendation-output

In this module, you created a campaign by deploying your solution version. Then, you waited for the status of your campaign to be active. Finally, you used your campaign to get movie title recommendations for a random user with Amazon Personalize.

In the next module, you clean up the resources you created for this lab.