<jupyter_start><jupyter_text>Starbucks - XGBoost    Udacity - Machine Learning Engineer Nanodegree Program    Author: <NAME>    **Here the XGBoost algorithm will be used to perform a multilabel classification of the type of offer. The dataset used will only be 'offers'. At the end of the process, the accuracy of the model will be calculated.**<jupyter_code># imports
import os
import numpy as np
import pandas as pd
import boto3
import sagemaker
from sagemaker.amazon.amazon_estimator import get_image_uri 
import matplotlib.pyplot as plt
#from sagemaker.xgboost.estimator import XGBoost<jupyter_output><empty_output><jupyter_text>AWS Sagemaker configurations<jupyter_code># session and role
sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()

# create an S3 bucket
bucket = sagemaker_session.default_bucket()
# should be the name of directory you created to save your features data
data_dir = 'data'

# set prefix, a descriptive name for a directory  
prefix = 'data'

# upload all data to S3
input_data = sagemaker_session.upload_data(path=data_dir, bucket=bucket, key_prefix=prefix)
input_data
output_path = 's3://{}/{}'.format(bucket, prefix)
output_path<jupyter_output><empty_output><jupyter_text>Train and test data<jupyter_code># confirm that data is in S3 bucket
empty_check = []
for obj in boto3.resource('s3').Bucket(bucket).objects.all():
    empty_check.append(obj.key)
    print(obj.key)

assert len(empty_check) !=0, 'S3 bucket is empty.'
print('Test passed!')
x_test_path = output_path + '/X_test.csv'
x_test = pd.read_csv(x_test_path, names=None, dtype='float32')
x_test.head()
test_x_np = x_test.to_numpy()
test_x_np
y_test_path = output_path + '/y_test.csv'
y_test = pd.read_csv(y_test_path, names=None, dtype='float32')
y_test.head()
test_y_np = y_test.iloc[:,0].to_numpy()
test_y_np<jupyter_output><empty_output><jupyter_text>Train model<jupyter_code>container = get_image_uri(boto3.Session().region_name,
                          'xgboost', 
                          repo_version='1.0-1')
xgb = sagemaker.estimator.Estimator(image_name=container, 
                                    role = role, 
                                    train_instance_count=1, 
                                    train_instance_type='ml.m4.xlarge',
                                    output_path=output_path,
                                    sagemaker_session=sagemaker_session)
xgb.set_hyperparameters(max_depth=5,
                        eta=0.2,
                        gamma=4,
                        min_child_weight=6,
                        subsample=0.8,
                        silent=0,
                        objective='multi:softmax',
                        num_round=100,
                        num_class=3)
#formatted_train_data = xgb.record_set(train_x_np, labels=train_y_np)
#s3_input_train = sagemaker.s3_input(s3_data=output_path, content_type='csv')
#s3_input_train2 = sagemaker.s3_input(s3_data=output_path + '/train.csv', content_type='csv')
s3_input_train3 = sagemaker.s3_input(s3_data=input_data + '/train.csv', content_type='csv')
%%time
xgb.fit({'train': s3_input_train3});
%%time
# deploy your model to create a predictor
predictor = xgb.deploy(initial_instance_count=1, instance_type='ml.t2.medium')
from sagemaker.predictor import csv_serializer

predictor.content_type = 'text/csv'
predictor.serializer = csv_serializer
predictor.deserializer = None
s3_input_test = sagemaker.s3_input(s3_data=input_data + '/X_test.csv', content_type='csv')
test_preds = predictor.predict(x_test.values).decode('utf-8')
test_y_preds = np.fromstring(test_preds, sep=',')
test_y_preds
# test that your model generates the correct number of labels
assert len(test_y_preds)==len(test_y_np), 'Unexpected number of predictions.'
print('Test passed!')<jupyter_output>Test passed!<jupyter_text>Model evaluation<jupyter_code>from sklearn.metrics import accuracy_score

accuracy = accuracy_score(test_y_np, test_y_preds, normalize=True)
print(accuracy)

print('\nPredicted class labels: ')
print(test_y_preds)
print('\nTrue class labels: ')
print(test_y_np)
pip install mlxtend
#contrução da matriz de confusão
from sklearn.metrics import classification_report, confusion_matrix
matriz_confusao = confusion_matrix(test_y_np, test_y_preds)
print(matriz_confusao)
#realiza o plot da matriz de confusão
from mlxtend.plotting import plot_confusion_matrix
fig, ax = plot_confusion_matrix(conf_mat=matriz_confusao)
plt.show()<jupyter_output><empty_output><jupyter_text>Delete endpoint and empty bucket<jupyter_code>#delete endpoint
predictor.delete_endpoint()
# deleting bucket
bucket_to_delete = boto3.resource('s3').Bucket(bucket)
bucket_to_delete.objects.all().delete()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09786964775363848, 'fraction_numerical': 0.01117907614427336, 'mean_word_length': 4.182513661202186, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '236242', 'license': 'MIT', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1557, 'n_words': 325, 'path': '6. Starbucks - XGBoost.ipynb', 'repo_name': 'leosimoes/Udacity-Starbucks', 'size': '4750'}