<jupyter_start><jupyter_text>Purpose: Demonstrate Model Creation using AWS boto3 sdk using Iris Multi-class datasetSteps:1. Upload Training Set and Training Schema files to S32. Create Training Data Source, Evaluation Data Source3. Create MultiClass Model using Training Data Source4. Create Evaluation using  Evaluation Data Source5. Get Status and Print Performance Metrics Avg. F1 Score<jupyter_code>import boto3
import os<jupyter_output><empty_output><jupyter_text>Machine Learning boto3 API Reference:http://boto3.readthedocs.io/en/latest/reference/services/machinelearning.htmlMachineLearning.Client.create_ml_modelS3:http://boto3.readthedocs.io/en/latest/reference/services/s3.htmlS3.Bucket.upload_file<jupyter_code># Local Data Path
# Training, Eval Files
# Batch Evaluation Files
# Download Batch result to local drive
data_path = r'C:\AWSMLCourse\Data\ModelCreationDemo'
# Training and Test Files
training_file = 'iris_data_train.csv'
training_schema_file = 'iris_data_train.csv.schema'
batch_test_file = 'iris_data_classifier_test.csv'
# S3 Bucket name - bucket name needs to be globally unique across AWS (not just your account)
# Example (make sure you modify it to point to your s3 bucket)
s3_bucket_name = 'my-ml-demo'
s3_folder_name = 'boto3_demo' 
# s3_path will be s3_bucket_name/s3_folder_name/
# Initialize Session with appropriate user profile.
# Optionally, use a different profile (profile_name='mluser') instead of default profile
# Specify region where you want to create your ML Models and files

# http://docs.aws.amazon.com/machine-learning/latest/dg/regions-and-endpoints.html
# currently ML is available in US East (N. Virginia)	us-east-1 and EU (Ireland)	eu-west-1

session   = boto3.Session(region_name='us-east-1',profile_name='ml_user')
ml_client = session.client('machinelearning' )
def upload_files_to_s3():    
    s3Client = session.resource('s3')
    fileNames = [training_file, training_schema_file, batch_test_file]
    for fileName in fileNames:
        filePath = os.path.join(data_path,fileName)
        print(filePath)        
        # upload files to s3 bucket.
        s3Client.Bucket(s3_bucket_name).upload_file(filePath, s3_folder_name + '/' + fileName)
upload_files_to_s3()
# Derived from
#https://github.com/awslabs/machine-learning-samples/blob/master/social-media/create-aml-model.py
def create_data_source(dataset_name, 
                       s3_data_uri, s3_schema_uri, 
                       ds_type, percent_begin, 
                       percent_end, 
                       compute_statistics):
    ds_id = "ds-boto3-iris-{0}".format(ds_type)
    data_spec = {}
    data_spec['DataLocationS3'] = s3_data_uri
    data_spec['DataSchemaLocationS3'] = s3_schema_uri
    data_spec['DataRearrangement'] = \
        '{{"splitting":{{"percentBegin":{0},"percentEnd":{1},"strategy":"sequential"}}}}'.format(
        percent_begin, percent_end)
    
    response = ml_client.create_data_source_from_s3(
        DataSourceId=ds_id,
        DataSourceName="{0}_[percentBegin={1}, percentEnd={2}]".format(dataset_name, percent_begin, percent_end),
        DataSpec=data_spec,    
        ComputeStatistics=compute_statistics)
    
    print("Creating {0} datasource".format(ds_type))
    return response
# Create Training Data Source
s3_train_uri = "s3://{0}/{1}/{2}".format(s3_bucket_name, s3_folder_name,training_file)
s3_train_schema_uri = "s3://{0}/{1}/{2}".format(s3_bucket_name, s3_folder_name,training_schema_file)
s3_train_uri, s3_train_schema_uri
train_datasource = create_data_source(
    'iris_training', 
    s3_train_uri,
    s3_train_schema_uri,
    'Training',0,70,True)
eval_datasource = create_data_source(
    'iris_evaluation', 
    s3_train_uri,
    s3_train_schema_uri,
    'Evaluation',70,100,False)
print(train_datasource['DataSourceId'])
print(eval_datasource['DataSourceId'])
model_create_response = ml_client.create_ml_model(
    MLModelId='ml-iris-demo',
    MLModelName='ML model: iris-demo-from-code',
    MLModelType='MULTICLASS',    
    TrainingDataSourceId=train_datasource['DataSourceId'])
model_create_response
# Query 'Status': 'PENDING'|'INPROGRESS'|'FAILED'|'COMPLETED'|'DELETED', 
ml_client.get_ml_model(MLModelId = model_create_response['MLModelId'])['Status']
evaluation_response = ml_client.create_evaluation (
    EvaluationId='eval-iris-demo',
    EvaluationName='Eval ML model: iris-demo-from-code',
    MLModelId = model_create_response['MLModelId'],    
    EvaluationDataSourceId=eval_datasource['DataSourceId'])
evaluation_response
#Query 'Status': 'PENDING'|'INPROGRESS'|'FAILED'|'COMPLETED'|'DELETED'
eval_result = ml_client.get_evaluation(EvaluationId=evaluation_response['EvaluationId'])
eval_result['Status']
eval_result['PerformanceMetrics']
eval_result<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10904533110507625, 'fraction_numerical': 0.016294129935241278, 'mean_word_length': 5.107142857142857, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '97003', 'license': 'MIT', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1573, 'n_words': 286, 'path': 'Udemy Course/15. Integration of AWS Machine Learning with your Application/boto_ml_integration_create_model.ipynb', 'repo_name': 'Kshitij68/AWS_tuts', 'size': '4787'}