Overview

Make predictions with with the SageMaker Endpoint that you have setup after downloading the model package from AWS Marketplace. The model takes a range of stats from both home and away teams and predicts the if a team will win or lose along with the confidence of it's prediction. This document will mainly explain for you can make predictions with ThingLogix's Foundry but it will also go over how to make predictions if you have not installed Foundry. 


Setup Lambda Functions

The setup involves creating three lambda functions. Two which help in the process of generating the most up-to-date stats for the teams and a third for actually making the predictions. I've attached the python 3.6 deployment packages for all three Lambda functions. To install them all that is needed is to upload them to Lambda and change a few attributes like the function handler, environment variables, and timeout.

1. generate-weekly-stats-nfl.zip

This function is primarily a helper function which then calls the lambda function generate-nfl-stats.

Name: generate-weekly-stats-nfl

Runtime: Python 3.6

Handler: lambda_function.lambda_handler

Timeout: 2min

Environment Variables: 

    'TYPE': Either 'Foundry' if your using Foundry to make your predictions otherwise use 'Normal'


2. generate-weekly-stats-nfl.zip

This function is used to webscrape the the site https://www.pro-football-reference.com to grab the most up-to-date stats for each team which then will be used for predictions. If using Foundry the stats will sent as a MQTT message. If you are not using Foundry if will simply print the data array for the team which will used as half the data for the making a prediction. 

Name: generate-weekly-stats-nfl

Runtime: Python 3.6

Handler: get_stats.lambda_handler

Timeout: 5min


3. nfl-make-prediction.zip

This function is actually used to make the predictions with the SageMaker endpoint you have already setup. 

Name: generate-weekly-stats-nfl

Runtime: Python 3.6

Handler: make_prediction.lambda_handler

Timeout: 1min

Environment Variables: 

    'ENDPOINT': Name of your SageMaker endpoint

    'ENV': This is only needed if using Foundry but the use either 'dev', 'stage', or 'prod' depending what instance you are using.


Setup for Foundry (Optional)

If you plan on using Foundry to manage making your predictions and updating your data sets then there a few additional steps that must be done. Here we will be uploading another Lambda deployment package and then running in it with a few specific attributes to generate objects and types for Foundry.

Install Init Lambda and Run:

Package: init-nfl-types-and-actions.zip

Name: init-nfl-types-and-actions

Runtime: Python 3.6

Handler: lambda_handler.lambda_handler

Timeout: 1min

Environment Variables: 

    'ENV': This is only needed if using Foundry but the use either 'dev', 'stage', or 'prod' depending what instance you are using.


Run Lambda Function

Now you'll need to run the function with three different arguments to generate the necessary objects and types.

Arguments:

{
  "accountId": Foundry accountId,
  "predictionArn": Lambda Arn for nfl-make-prediction,
  "weeklyArn": Lambda Arn for generate-weekly-nfl-stats
}


Create Auto Provisioning Rule

Lastly we will create an AP rule to automatically create stat objects for NFL to use for predictions. For more info on how to create an AP rule check out the solutions document here.

Rule Arguments:




Running Predictions Foundry

1. Run the Lambda function generate-weekly-stats-nfl manually. It does not require any arguments. This will grab all the most up-to-date stats for NFL teams and store them as a object in Foundry for easy use for predictions.

2. Create a Object of NFLPrediction and add your home and away teams.


3. From the Advanced Search perform a Lambda invoke action on the newly created object with the Global action "Make NFL Prediction"


4. View the results.



Running Predictions without Foundry

1. In the Lambda function generate-nfl-stats, fun the function with an argument of your favorite team and type of 'Normal'. Note: The team name must be in the same format as this list Dolphins, Patriots, Jets, Bills, Ravens, Bengals, Browns, Steelers, Jaguars, Titans, Colts, Texans, Chiefs, Broncos, Chargers, Raiders, Redskins, Eagles, Cowboys, Giants, Bears, Packers, Vikings, Lions, Saints, Panthers, Buccaneers, Falcons, Rams, Seahawks, 49ers, Cardinals.

Payload Example: 

{
  "team": "Raiders",
  "type": "Normal"
}

Using the response: The response will return an array of stats.

Example Response: 

[342.4, 6579.2, 1078.4, '6.1', 28.8, 3.2, 342.4, 460.8, 646.4, 5056.0, 22.4, 25.6, '7.4', 236.8, 396.8, 1523.2, 9.6, '3.8', 76.8, 115.2, 1097.6, 28.8, 192.0, '33.3', '15.0', '26.1', '2:45', '5.85', '34.3', '1.67', 476.8, 6470.4, 947.2, '6.8', 16.0, 6.4, 307.2, 313.6, 512.0, 4435.2, 32.0, 9.6, 26.56, 188.8, 416.0, 2035.2, 19.2, '4.9', 96.0, 128.0, 1011.2, 22.4, 192.0, '43.3', '8.3', '28.5', '2:23', '5.2', '33.7', '2.37', '22', '6', '23', '2', '21', '8', '6', '22', '31', '6', '19', '24', '18', '24', '20', '7', '27', '13', '20', '11', '25', '30', '30', '24', '19', '9', '2', '23', '20', '21', '31', '18', '28', '27', '27', '6', '26', '15', '5', '2', '21', '28', 195.2, 76.8, '39.3', 12.8, 3.2, '25.0', 60.8, 28.8, '47.4', 201.6, 83.2, '41.3', 6.4, 6.4, '100.0', 57.6, 28.8, '50.0', '18', '30', '25', '20', '29', '11', 27.0, 16.0, 460.8, 646.4, '71.3', 5251.2, 22.4, 11.2, 25.6, '4.0', '66', '8.1', '7.0', '11.4', '328.2', '90.4', 35.2, 195.2, '7.42', '6.38', '5.2', 3.2, 3.2, 16.0, 313.6, 512.0, '61.3', 4576.0, 32.0, 20.16, 9.6, '1.9', '8.9', '9.34', '14.6', '286.0', '103.4', 19.2, 140.8, '8.3', '8.7', '3.6', 28.54, 16.0, 396.8, 1523.2, 9.6, 166.4, '3.8', '95.2', '24.8', 627.2, 460.8, 5251.2, '11.4', 22.4, 211.2, '28.8', '328.2', 857.6, '7.9', 6774.4, 32.0, 6.4, 16.0, 416.0, 2035.2, 19.2, '4.9', '127.2', '26.0', 313.6, 4435.2, '14.1', 32.0, '19.6', '277.2', 6470.4, 51.2, 12.8, 32.0, 16.0, 35.2, 409.6, 0.0, 156.8, '11.6', 12.8, 265.6, 0.0, 70.4, '20.8', 7612.8, 16.0, 28.8, 185.6, 0.0, '6.4', 44.8, 1020.8, 0.0, '22.8', 27.67, 16.0, 0.0, 0.0, 19.2, 19.2, 0.0, 0.0, 12.8, 9.6, 9.6, 3.2, 41.6, 32.0, '76.9%', 32.0, 28.8, '90.0%', 67.2, 3017.6, 179.2, 0.0, '44.9', 16.0, 38.4, 32.0, '83.3%', 44.8, 41.6, '92.9%', 70.4, 3462.4, '49.2', 28.16, 16.0, 9.6, 163.2, 3.2, 115.2, 89.6, 9.6, 6.4, 9.6, 0.0, 0.0, 19.2, 592.0, 201.6, 0.0, 16.0, 12.8, 9.6, 35.2, 0.0, 0.0, 0.0, 0.0, 0.0, '15', 51.2]



2. Combining returned stats from another team. In order to run a prediction you must generate the stats for two teams and combine them into one array where the home team stats come first. It should be a single array not an array of two arrays.

[home team stats, away team stats]


3. Using the combined array you can use it as an argument for the nfl-make-prediction function:

{
    "data_arr": [combined array of stats]
}


4. After you have ran the function it will return a prediction in the following format. Where the score is the prediction confidence and predicted label is the prediction. 1 == Home team will win. 0 == Home team will lose.

{'score': 0.4826793372631073, 'predicted_label': 1.0}


Cleanup

Once finished making your predictions it is recommended to delete your endpoint to reduce AWS costs.