<jupyter_start><jupyter_text>Automated MLTODO: Import Dependencies. In the cell below, import all the dependencies that you will need to complete the project.<jupyter_code>import logging

from matplotlib import pyplot as plt
import pandas as pd
import os

import azureml.core
from azureml.core.experiment import Experiment
from azureml.core.workspace import Workspace
from azureml.automl.core.featurization import FeaturizationConfig
from azureml.core.dataset import Dataset
from azureml.train.automl import AutoMLConfig
from azureml.interpret import ExplanationClient
from azureml.automl.runtime.onnx_convert import OnnxConverter
from azureml.data.dataset_factory import TabularDatasetFactory
from sklearn.preprocessing import OneHotEncoder
import pandas as pd
from azureml.core.run import Run
from sklearn.model_selection import train_test_split
from azureml.widgets import RunDetails
from azureml.train.sklearn import SKLearn

print("SDK version:", azureml.core.VERSION)<jupyter_output>SDK version: 1.19.0<jupyter_text>Dataset OverviewTODO: In this markdown cell, give an overview of the dataset you are using. Also mention the task you will be performing.TODO: Get data. In the cell below, write code to access the data you will be using in this project. Remember that the dataset needs to be external.<jupyter_code>ws = Workspace.from_config()
ws.write_config(path='.azureml')

# choose a name for experiment
experiment_name = 'automl-classification-experiment'

experiment=Experiment(ws, experiment_name)

print('Workspace name: ' + ws.name, 
      'Azure region: ' + ws.location, 
      'Subscription id: ' + ws.subscription_id, 
      'Resource group: ' + ws.resource_group, sep = '\n')

run = experiment.start_logging()
from azureml.core.compute import ComputeTarget, AmlCompute
from azureml.core.compute_target import ComputeTargetException

cpu_cluster_name = "cpu-cluster-1"
try:
    compute_target = ComputeTarget(workspace=ws, name=cpu_cluster_name)
    print('Found existing cluster')
except ComputeTargetException:
    compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_D2_V2', max_nodes=4)
    compute_target = ComputeTarget.create(ws, cpu_cluster_name, compute_config)
    compute_target.wait_for_completion(show_output=True)
key = 'uciheartfailuredataset'
if key in ws.datasets.keys(): 
    dataset = ws.datasets[key] 
    print("Found dataset")
else:
    url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00519/heart_failure_clinical_records_dataset.csv'
    dataset = Dataset.Tabular.from_delimited_files(url)
    dataset = dataset.register(workspace=ws, name=key)
ds = dataset.to_pandas_dataframe()
train, test = train_test_split(ds, random_state=625, shuffle=True)
train.to_csv('train.csv', index=False)
datastore = ws.get_default_datastore()
datastore.upload_files(files = ['./train.csv'])
train = Dataset.Tabular.from_delimited_files(path = [(datastore,'train.csv')])<jupyter_output>Uploading an estimated of 1 files
Uploading ./train.csv
Uploaded ./train.csv, 1 files out of an estimated total of 1
Uploaded 1 files<jupyter_text>AutoML ConfigurationTODO: Explain why you chose the automl settings and cofiguration you used below.<jupyter_code># TODO: Put your automl settings here
automl_settings = {
    "experiment_timeout_hours" : 0.5,
    "enable_early_stopping" : True,
    "iteration_timeout_minutes": 5,
    "max_concurrent_iterations": 4,
    "max_cores_per_iteration": -1,
    "primary_metric": 'accuracy',
    "featurization": 'auto',
    "verbosity": logging.INFO,
}

# TODO: Put your automl config here
automl_config = AutoMLConfig(
    experiment_timeout_minutes=30,
    debug_log = 'automl_errors.log',
    compute_target=compute_target,
    task="classification",
    training_data= train,
    label_column_name="DEATH_EVENT",
    enable_onnx_compatible_models=True,
    n_cross_validations= 3,
    **automl_settings)
# TODO: Submit your experiment
remote_run = experiment.submit(automl_config,show_output = False)<jupyter_output>Running on remote.<jupyter_text>Run DetailsOPTIONAL: Write about the different models trained and their performance. Why do you think some models did better than others?TODO: In the cell below, use the `RunDetails` widget to show the different experiments.<jupyter_code>RunDetails(remote_run).show()
%%time
remote_run.wait_for_completion()<jupyter_output>CPU times: user 35.5 ms, sys: 0 ns, total: 35.5 ms
Wall time: 5.06 s<jupyter_text>Best ModelTODO: In the cell below, get the best model from the automl experiments and display all the properties of the model. Retrieve the Best ONNX Model<jupyter_code>best_run, onnx_model= remote_run.get_output(return_onnx_model=True)
#TODO: Save the best model
OnnxConverter.save_onnx_model(onnx_model, file_path="./automl_model.onnx")

best_run.get_properties()
best_run.get_metrics()
print(best_run)<jupyter_output>Run(Experiment: automl-classification-experiment,
Id: AutoML_efa4ac15-967d-4b8c-83d7-7c3513e695ab_46,
Type: azureml.scriptrun,
Status: Completed)<jupyter_text>Model DeploymentRemember you have to deploy only one of the two models you trained.. Perform the steps in the rest of this notebook only if you wish to deploy this model.TODO: In the cell below, register the model, create an inference config and deploy the model as a web service.<jupyter_code>model_name = best_run.properties['model_name']
script_file_name = 'inference/score.py'
best_run.download_file('outputs/scoring_file_v_1_0_0.py', 'inference/score.py')
description = 'AutoML Model'
tags = None
model = remote_run.register_model(model_name = model_name, description = description, tags = tags)
print(remote_run.model_id) 
from azureml.core.model import InferenceConfig
from azureml.core.webservice import AciWebservice
from azureml.core.webservice import Webservice
from azureml.core.model import Model
from azureml.core.environment import Environment

inference_config = InferenceConfig(entry_script='inference/score.py')

aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, 
                                               memory_gb = 1, 
                                               tags = {'type': "automl_classification"}, 
                                               description = 'Heart Data Service')

aci_service_name = 'automl-uciheart1'
print(aci_service_name)
aci_service = Model.deploy(ws, aci_service_name, [model], inference_config, aciconfig)
aci_service.wait_for_deployment(True)
print(aci_service.state)<jupyter_output>automl-uciheart1
Tips: You can try get_logs(): https://aka.ms/debugimage#dockerlog or local deployment: https://aka.ms/debugimage#debug-locally to debug if deployment takes longer than 10 minutes.
Running.........................................................
Succeeded
ACI service creation operation finished, operation "Succeeded"
Healthy<jupyter_text>Enable Application Insitghts on The Deployed Service<jupyter_code>aci_service.update(enable_app_insights=True)<jupyter_output><empty_output><jupyter_text>Service Details<jupyter_code>print(f'\nservice state: {aci_service.state}\n')
print(f'scoring URI: \n{aci_service.scoring_uri}\n')
print(f'swagger URI: \n{aci_service.swagger_uri}\n')<jupyter_output>service state: Healthy

scoring URI: 
http://5712d97a-c700-438b-b372-5c7c4929c5f1.southcentralus.azurecontainer.io/score

swagger URI: 
http://5712d97a-c700-438b-b372-5c7c4929c5f1.southcentralus.azurecontainer.io/swagger.json<jupyter_text>Test Service : TODO: In the cell below, send a request to the web service you deployed to test it.<jupyter_code>import requests
import json

scoring_uri = aci_service.scoring_uri
# Two sets of data to score, so we get two results back
data = {"data":
        [
          { 
            "age": 45,
            "anaemia": 0,
            "creatinine_phosphokinase": 2413,
            "diabetes": 0,
            "ejection_fraction": 38,
            "high_blood_pressure": 0,
            "platelets": 140000,
            "serum_creatinine": 1.4,
            "serum_sodium": 140,
            "sex": 1,
            "smoking": 1,
            "time": 280
          },
          {
            "age": 50,
            "anaemia": 0,
            "creatinine_phosphokinase": 196,
            "diabetes": 0,
            "ejection_fraction": "45",
            "high_blood_pressure": 0,
            "platelets": 395000,
            "serum_creatinine": 1.6,
            "serum_sodium": 136,
            "sex":1,
            "smoking": 1,
            "time": 285
          },
      ]
    }
# Convert to JSON string
input_data = json.dumps(data)
# Set the content type
headers = {'Content-Type': 'application/json'}
# Make the request and display the response
resp = requests.post(scoring_uri, input_data, headers=headers)
print(resp.json())<jupyter_output>{"result": [0, 0]}<jupyter_text>TODO: In the cell below, print the logs of the web service and delete the service<jupyter_code>aci_service.get_logs()
aci_service.delete()
model.delete()
compute_target.delete()<jupyter_output>Current provisioning state of AmlCompute is "Deleting"
{'fraction_non_alphanumeric': 0.09795467108899945, 'fraction_numerical': 0.018684355997788834, 'mean_word_length': 4.355831853167555, 'pattern_counts': {'":': 32, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 3, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '425281', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2809, 'n_words': 713, 'path': 'automl.ipynb', 'repo_name': 'ashishsomvanshi/nd00333-capstone', 'size': '9045'}