<jupyter_start><jupyter_text>XGBoost Endpoint - Multiple Models hosted on same instanceInvoke Specific Model<jupyter_code>import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
import os
import json

import boto3
import re
from sagemaker import get_execution_role
import sagemaker

# SDK 2 serializers and deserializers
from sagemaker.serializers import CSVSerializer
from sagemaker.deserializers import JSONDeserializer
# We need to specify the location of each model - relative path is fine
# TODO - Update your path here
targetModels = ["xgboost-bikerental-hyper-one-2021-06-29-19-03-51-772/output/model.tar.gz",
                "xgboost-bikerental-hyper-two-2021-06-29-19-08-04-688/output/model.tar.gz"]
# Create a predictor and point to an existing endpoint (note this endpoint has two versions of the model)
endpoint_name = 'xgboost-bikerental-hyper'
predictor = sagemaker.predictor.Predictor (endpoint_name=endpoint_name)
predictor.serializer = CSVSerializer()
# Read Test Data
df_all = pd.read_csv('bike_test.csv')
df_all.head()
# Need to pass an array to the prediction
# can pass a numpy array or a list of values [[19,1],[20,1]]
arr_test = df_all[df_all.columns[1:]].values
arr_test.shape<jupyter_output><empty_output><jupyter_text>Invoke Model One<jupyter_code># target_model = path to the model artifact. For multi-model endpoints, we need to provide the path to the model artifact
# Call the first model
# Output is a JSON List
result = predictor.predict(
    arr_test[:5], 
    target_model=targetModels[0])

print(result)<jupyter_output>b'[2.332122325897217, 1.9005593061447144, 1.6316101551055908, 1.0376962423324585, 0.9574321508407593]'<jupyter_text>Invoke Model Two<jupyter_code># target_model = path to the model artifact. For multi-model endpoints, we need to provide the path to the model artifact
# Call the second model
# Output is a JSON List
result = predictor.predict(
    arr_test[:5], 
    target_model=targetModels[1])

print(result)<jupyter_output>b'[2.489321708679199, 1.7995195388793945, 1.92416250705719, 1.335949182510376, 1.1886606216430664]'<jupyter_text>Split the input data into chunksThere are thousands of rows in this data set for which need inference.  When communicating over internet, it is a good idea to split the data into chunks to prevent payload and timeout error<jupyter_code>def inference_by_version(targetModel = None):
    # For large number of predictions, we can split the input data and
    # Query the prediction service.
    # array_split is convenient to specify how many splits are needed
    predictions = []
    for arr in np.array_split(arr_test,10):
        result = predictor.predict(arr, target_model=targetModel)
        result = json.loads(result.decode("utf-8"))
        print (arr.shape)
        predictions += [float(r) for r in result]
        
    return predictions<jupyter_output><empty_output><jupyter_text>Use all available variants<jupyter_code>print('model one inference')
df_all["count_hyper_one"] = np.expm1(inference_by_version(targetModel=targetModels[0]))
print('model two inference')
df_all["count_hyper_two"] = np.expm1(inference_by_version(targetModel=targetModels[1]))
df_all[["count_hyper_one","count_hyper_two"]].describe()
# Delete Endpoint to prevent unnecessary charges
predictor.delete_endpoint()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08685306365258774, 'fraction_numerical': 0.06513979773944081, 'mean_word_length': 5.430210325047801, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '339793', 'license': 'Apache-2.0', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1055, 'n_words': 310, 'path': '5 Xgboost/BikeSharingRegression/multiple_models_prediction.ipynb', 'repo_name': 'jaypeeml/AWSSagemaker', 'size': '3362'}