<jupyter_start><jupyter_text>CatBoost XGBoost Script Mode Training and Serving This is a sample Python program that trains a simple CatBoost model and a XGBoost model using SageMaker XGBoost Docker image, and then performs inference. This implementation will work on your *local computer* or in the *AWS Cloud*. Prerequisites:1. Install required Python packages:   `pip install -r requirements.txt`2. Docker Desktop installed and running on your computer:   `docker ps`3. You should have AWS credentials configured on your local machine in order to be able to pull the docker image from ECR.<jupyter_code>import os
import sagemaker
import pandas as pd
from sagemaker.predictor import csv_serializer
from sagemaker.xgboost import XGBoost
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
sess = sagemaker.Session()
bucket = sess.default_bucket()
role = sagemaker.get_execution_role()

    
prefix = "xgboost_catboost"<jupyter_output><empty_output><jupyter_text>Downloading DataDownload training and eval data<jupyter_code>local_train = './data/train/boston_train.csv'
local_validation = './data/validation/boston_validation.csv'
local_test = './data/test/boston_test.csv'
if os.path.isfile('./data/train/boston_train.csv') and \
        os.path.isfile('./data/validation/boston_validation.csv') and \
        os.path.isfile('./data/test/boston_test.csv'):
    print('Training dataset exist. Skipping Download')
else:
    print('Downloading training dataset')

    os.makedirs("./data", exist_ok=True)
    os.makedirs("./data/train", exist_ok=True)
    os.makedirs("./data/validation", exist_ok=True)
    os.makedirs("./data/test", exist_ok=True)

    data = load_boston()

    X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.25, random_state=45)
    X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.5, random_state=45)

    trainX = pd.DataFrame(X_train, columns=data.feature_names)
    trainX['target'] = y_train

    valX = pd.DataFrame(X_test, columns=data.feature_names)
    valX['target'] = y_test

    testX = pd.DataFrame(X_test, columns=data.feature_names)

    trainX.to_csv(local_train, header=None, index=False)
    valX.to_csv(local_validation, header=None, index=False)
    testX.to_csv(local_test, header=None, index=False)

    print('Downloading completed')<jupyter_output>Training dataset exist. Skipping Download<jupyter_text>Model TrainingStarting model training using **local mode**. Note: if launching for the first time in local mode, container image download might take a few minutes to complete.<jupyter_code>training_instance_type = "ml.m5.xlarge"
train_location = sess.upload_data(
    local_train, key_prefix="{}/data/{}".format(prefix, "train")
)
validation_location = sess.upload_data(
    local_validation, key_prefix="{}/data/{}".format(prefix, "validation")
)
        
hyperparameters = {"num_round": 6}

estimator_parameters = {
    "entry_point": "multi_model_deploy.py",
    "source_dir": "code",
    "dependencies": ["my_custom_library"],
    "instance_type": training_instance_type,
    "instance_count": 1,
    "hyperparameters": hyperparameters,
    "role": role,
    "base_job_name": "xgboost-model",
    "framework_version": "1.0-1",
    "py_version": "py3",
}    
    

estimator = XGBoost(**estimator_parameters)

estimator.fit({'train': train_location, 'validation': validation_location})
print('Completed model training')

model_data = estimator.model_data
model_data<jupyter_output><empty_output><jupyter_text>Deploying trained model We can also deploy the trained model and perform invocation uncomment the below cell if you would like to deploy directly from the estimator object.<jupyter_code># endpoint_name = "xgboost-catboost-endpoint"
# predictor = estimator.deploy(
#         initial_instance_count=1, instance_type="ml.m5.xlarge", endpoint_name=endpoint_name
#     )<jupyter_output><empty_output><jupyter_text>If you already have a model trained previously, you can use the model s3 uri in the model_data field and create a model object for deployment. No need to retrain the model using the estimator.<jupyter_code>from sagemaker.xgboost.model import XGBoostModel

inference_model = XGBoostModel(
    model_data=model_data,
    role=role,
    entry_point="multi_model_deploy.py",
    framework_version="1.0-1",
    dependencies=["my_custom_library"],
    source_dir="code",
)<jupyter_output><empty_output><jupyter_text>The entry point script "multi_model_deploy.py" will handle the multiple models in the model artifacts and perform inference against each model. The results will be the mean of each inference output. This is a simple demonstration of how to work with multiple models, but you can design the model ensemble as you need.<jupyter_code>predictor = inference_model.deploy(
    initial_instance_count=1,
    instance_type="ml.m5.xlarge",
)

from sagemaker.serializers import NumpySerializer, JSONSerializer, CSVSerializer
from sagemaker.deserializers import NumpyDeserializer, JSONDeserializer
predictor.serializer = CSVSerializer()
predictor.deserializer = JSONDeserializer()

with open(local_test, 'r') as f:
    payload = f.read().strip()

predictions = predictor.predict(payload)
print('predictions: {}'.format(predictions))<jupyter_output>predictions: [14.613732250744945, 6.962288966510727, 8.509503902178903, 13.653751169637694, 14.4809041819816, 10.950596754285558, 12.75489476110096, 12.860061052753862, 13.370978134023789, 12.781238049928602, 19.381960726603, 12.637171388576272, 12.342439986571616, 12.34379449229714, 12.742601145707347, 25.343351307891936, 18.26171883917135, 16.017435536833347, 17.724286971386697, 17.138903624278267, 12.167699802883755, 11.377891533820002, 15.634961368394507, 14.983961505461675, 6.394251938354945, 7.309393511752864, 10.49425297549001, 14.02178997586292, 12.455626175923042, 23.10296230497474, 9.848035070241453, 12.381957848097404, 14.504886544687285, 4.84468595075827, 13.234406309181658, 10.994238485286514, 25.807487982378284, 11.571759114652789, 9.309522345120484, 14.226494447635211, 5.236289432440608, 17.680829294308253, 11.036644747800972, 6.685870846321972, 11.831237074596912, 6.107978025842288, 23.58894916584692, 4.798365969192604, 9.72145147923905, 6.645664091897018, 25.5499996794[...]<jupyter_text>Clear up resourcesDelete the endpoint deployed in local<jupyter_code>predictor.delete_endpoint(predictor.endpoint)<jupyter_output>The endpoint attribute has been renamed in sagemaker>=2.
See: https://sagemaker.readthedocs.io/en/stable/v2.html for details.
{'fraction_non_alphanumeric': 0.1015767131594906, 'fraction_numerical': 0.13098847786537296, 'mean_word_length': 5.857588357588358, 'pattern_counts': {'":': 11, '<': 26, '<?xml version=': 0, '>': 27, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '541861', 'license': 'MIT-0', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2190, 'n_words': 499, 'path': 'multi_model_inference/catboost_xgboost_script_mode_local_training_and_serving.ipynb', 'repo_name': 'melanie531/Amazon-SageMaker-Bring-Your-Own-Scripts-and-Container', 'size': '6596'}