<jupyter_start><jupyter_text>Run a sktime model on Amazon SageMaker (Docker Mode)<jupyter_code># install/upgrade needed package
# please restart the notebook after the installation finished
!pip install -q -U sagemaker<jupyter_output>[33mWARNING: You are using pip version 21.1.3; however, version 21.2.4 is available.
You should consider upgrading via the '/home/ec2-user/anaconda3/envs/python3/bin/python -m pip install --upgrade pip' command.[0m<jupyter_text>Prepare our dataset<jupyter_code>import pandas as pd
import numpy as np

airline_dataset = pd.read_csv('../Sample-Data/airline.csv')
airline_dataset<jupyter_output><empty_output><jupyter_text>Make ECR Repo<jupyter_code>import sagemaker

session = sagemaker.Session()
bucket = session.default_bucket()
account_id = session.boto_session.client('sts').get_caller_identity()['Account']
region = session.boto_session.region_name
image_name = 'sktime-naive-forecaster-demo'<jupyter_output><empty_output><jupyter_text>Run follwoing cell just for the first time<jupyter_code>%%bash -s "$region" "$image_name"
# define variables
export REGION=$1
export IMAGE_NAME=$2
aws ecr create-repository --repository-name $IMAGE_NAME --region $REGION<jupyter_output><empty_output><jupyter_text>Puch Docker to ECR Repo<jupyter_code>%%bash -s "$region" "$image_name"
# define variables
export REGION=$1
export IMAGE_NAME=$2

# Bulid the image
export ACCOUNT_ID=`aws sts get-caller-identity --query Account --output text`
docker build -t $IMAGE_NAME:latest -f Dockerfile .
export IMAGE_ID=`docker images -q $IMAGE_NAME:latest`

docker tag $IMAGE_ID $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$IMAGE_NAME:latest
aws ecr get-login-password --region $REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$IMAGE_NAME:latest
docker push $ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$IMAGE_NAME:latest<jupyter_output><empty_output><jupyter_text>Create Training Job<jupyter_code>import sagemaker
from sagemaker.estimator import Estimator

                  
prefix = 'sktime-airline'

training = session.upload_data(path='../Sample-Data/airline.csv', key_prefix=prefix + "/training")
output   = 's3://{}/{}/output/'.format(bucket,prefix)

print(training)
print(output)

role = sagemaker.get_execution_role()

sk_training = Estimator(
             image_uri=f'{account_id}.dkr.ecr.{region}.amazonaws.com/{image_name}:latest',
             role=role,
             instance_count=1,
             instance_type='ml.m5.large',
             output_path=output,
             hyperparameters={
                  'normalize': True,
                  'test-size': 0.1,
                  'random-state': 123
             }
)

sk_training.fit({'training':training})
sk_predictor = sk_training.deploy(instance_type='ml.m5.large',
                                  initial_instance_count=1)

# set the predictor input/output configurations
sk_predictor.serializer = sagemaker.serializers.CSVSerializer()
sk_predictor.deserializer = sagemaker.deserializers.CSVDeserializer()<jupyter_output>-----------!<jupyter_text>Deploy model and see the predictions<jupyter_code>import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(16, 4))

x = airline_dataset.iloc[:,0].values
y = airline_dataset.iloc[:,1].values

x_train = x[:-36]
x_test = x[-36:]
y_train = y[:-36]
y_test = y[-36:]

fh = np.arange(1, len(y_test) + 1)

y_pred = np.array([float(i[0]) for i in sk_predictor.predict(fh)])

x_ticks = [x[i] if i%5 == 0 else '' for i in range(len(x))] 

plt.plot(x_train, y_train, 'o-', c='#28334AFF', label='Training')
plt.plot(x_test, y_test, 'o-', c='#FBDE44FF', label='True')
plt.plot(x_test, y_pred, 'o-', c='#F65058FF', label='Prediction')

plt.xticks(x, x_ticks, rotation=45)
plt.legend()
plt.show()
sk_predictor.delete_endpoint()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13553113553113552, 'fraction_numerical': 0.015698587127158554, 'mean_word_length': 4.688988095238095, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '260183', 'license': 'MIT', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1328, 'n_words': 261, 'path': 'sktime_on_sagemaker/Docker-Mode/docker-mode-notebook.ipynb', 'repo_name': 'DataChefHQ/BlogProjects', 'size': '3822'}