<jupyter_start><jupyter_text>Automated ML<jupyter_code>from azureml.core import Workspace, Experiment, Dataset
from azureml.core.compute import AmlCompute
from azureml.core.compute import ComputeTarget
from azureml.core.compute_target import ComputeTargetException
from azureml.train.automl import AutoMLConfig
from azureml.widgets import RunDetails
from azureml.core.model import InferenceConfig 
from azureml.core.webservice import AciWebservice, Webservice
from azureml.core.model import Model

import pandas as pd
import warnings
warnings.filterwarnings("ignore")
ws = Workspace.from_config()
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\n')

experiment_name = 'capstone-heart-failure-prediction'
experiment=Experiment(ws, experiment_name)
experiment

run = experiment.start_logging()<jupyter_output>ws_udacity_capstone_v2
UdacityMLAzureCapstoneV2
eastus2
ca1598e0-85dc-47d5-b06d-41b5342b4989<jupyter_text>Create New Cluster / Use Existing Cluster<jupyter_code># Create compute cluster and choose a name for it
cpu_cluster_name = "CapstoneV1"

# Verify that cluster does not exist already
try:
    compute_target = ComputeTarget(workspace=ws, name=cpu_cluster_name)
    print('Found existing cluster, use it.')
except ComputeTargetException:
    print('Creating a new compute cluster...')
    compute_config = AmlCompute.provisioning_configuration(vm_size='Standard_DS3_v2', 
                                                            min_nodes=1, 
                                                            max_nodes=4)
    compute_target = ComputeTarget.create(ws, cpu_cluster_name, compute_config)

# Can poll for a minimum number of nodes and for a specific timeout. 
# If no min node count is provided it uses the scale settings for the cluster.
compute_target.wait_for_completion(show_output=True)

# use get_status() to get a detailed status for the current cluster. 
print(compute_target.get_status().serialize())<jupyter_output>Found existing cluster, use it.

Running
{'errors': [], 'creationTime': '2021-04-25T18:53:25.316093+00:00', 'createdBy': {'userObjectId': '2f5770ca-7bf7-4ecc-bd4b-14652b1bbf0c', 'userTenantId': '3814e260-63cb-43a8-82ce-e862c309e004', 'userName': '<NAME>'}, 'modifiedTime': '2021-04-25T18:55:27.349216+00:00', 'state': 'Running', 'vmSize': 'STANDARD_DS12_V2'}<jupyter_text>Dataset OverviewThis dataset used in this project is taken from [Kaggle](https://www.kaggle.com/andrewmvd/heart-failure-clinical-data). It consists of 12 distinct features and 1 target as summarized below:- **Input features** - Age, Anaemia, Creatinine-phosphokinase, Diabetes, Ejection_fraction, High_blood_pressure, Platelets, Serum_creatinine, Serum_sodium, Sex, Smoking, Time- **Target** - DEATH_EVENTWe will use this dataset for creating a model to predict mortality caused by heart failure.<jupyter_code>local_data_path = 'data/heart_failure_clinical_records_dataset.csv'

# get the datastore to upload prepared data
datastore = ws.get_default_datastore()

# upload the local file from src_dir to the target_path in datastore
datastore.upload(src_dir='data', target_path='data')

# create a dataset referencing the cloud location
heart_failure_ds= Dataset.Tabular.from_delimited_files(path = [(datastore, (local_data_path))])

# register dataset
heart_failure_ds = heart_failure_ds.register(workspace=ws, name='heart_failure_ds', create_new_version=True)

df = pd.read_csv(local_data_path)
df.head()
df.describe()<jupyter_output><empty_output><jupyter_text>AutoML ConfigurationI used the following automl settings and configuration parameters:```pythonautoml_settings = { "n_cross_validations": 2,                    "primary_metric": 'accuracy',                    "enable_early_stopping": True,                    "max_concurrent_iterations": 4,                    "experiment_timeout_minutes": 15,                }automl_config = AutoMLConfig(compute_target = compute_target,                            task='classification',                            training_data=heart_failure_ds,                            label_column_name='DEATH_EVENT',                            path = 'automl_runs',                            featurization= 'auto',                            debug_log = "automl_errors.log",                            enable_onnx_compatible_models=True,                            **automl_settings)```**AutoML Settings**- *n_cross_validations*: I chose 2 cross validations, which means that the metrics are calculated as an average of 2 folds.- *primary metric*: I chose `accuracy` as it is the default metric for classification tasks. - *enable_early_stopping*: I set this value to `True` so that the model can stop training once it stops improving.- *max_concurrent_iterations*: This value is set at 4, which means that there can be at max 4 iterations in parallel.- *experiment_timeout_minutes*: To save costs, I chose this value to be 15 mins. After this time the AutoML experiement will automatically stop.**AutoML Config**- *compute_target*: This defines the Azure Compute target that I set up for running this experiment.- *task*: Since this is a classification problem, this value is set as `classification`.- *training_data*: The training data used for this experiement. It contains both - training features and the target label.- *label_column_name*: Target label column name, which is `DEATH_EVENT`.- *path*: path to AzureML project folder.- *featurization*: Setting this value to `auto` means that featurization will be done automatically.- *debug_log*: path of the log file.- *enable_onnx_compatible_models*: Setting this value to `True` enables `onnx_compatible_models`.<jupyter_code>automl_settings = { "n_cross_validations": 2,
                    "primary_metric": 'accuracy',
                    "enable_early_stopping": True,
                    "max_concurrent_iterations": 10,
                    "experiment_timeout_minutes": 15,
                }

automl_config = AutoMLConfig(compute_target = compute_target,
                            task='classification',
                            training_data=heart_failure_ds,
                            label_column_name='DEATH_EVENT',
                            path = 'automl_runs',
                            featurization= 'auto',
                            debug_log = "automl_errors.log",
                            enable_onnx_compatible_models=True,
                            **automl_settings)<jupyter_output><empty_output><jupyter_text>Run Details<jupyter_code># Submit your experiment
automl_run = experiment.submit(automl_config, show_output=True)
RunDetails(automl_run).show()
RunDetails(automl_run).show()
automl_run.wait_for_completion()
print(f"Run Status: {automl_run.get_status()}")<jupyter_output>Run Status: Completed<jupyter_text>Best Model<jupyter_code>best_automl_run, fit_model = automl_run.get_output()
print("Best run metrics :",best_automl_run.get_metrics())
print("Best run properties :",best_automl_run.get_properties())

print("Best run details :", best_automl_run.get_details())

# download the environment file
best_automl_run.download_file('outputs/conda_env_v_1_0_0.yml', 'environment.yml')
print(fit_model)
# Save the best model

best_automl_run.download_file('outputs/model.pkl', 'best_automl_model.pkl')
print(best_automl_run)<jupyter_output>Run(Experiment: capstone-heart-failure-prediction,
Id: AutoML_352b5a23-e3b9-4455-bc61-d4e5ab1fb4ed_67,
Type: azureml.scriptrun,
Status: Completed)<jupyter_text>Model Deployment<jupyter_code>model = automl_run.register_model(model_name = 'best_automl_model.pkl')
print(automl_run.model_id)

environment = best_automl_run.get_environment()
entry_script='inference/scoring.py'
best_automl_run.download_file('outputs/scoring_file_v_1_0_0.py', entry_script)

inference_config = InferenceConfig(entry_script = entry_script, environment = environment)

# Deploy model using ACI WebService

deployment_config = AciWebservice.deploy_configuration(cpu_cores = 1, 
                                                    memory_gb = 1, 
                                                    auth_enabled= True, 
                                                    enable_app_insights= True)

service = Model.deploy(ws, "aciservice", [model], inference_config, deployment_config)
service.wait_for_deployment(show_output = True)
%run endpoint.py
# Printing the logs
print(service.get_logs())
service.delete()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08873356237412629, 'fraction_numerical': 0.01966591636062078, 'mean_word_length': 3.7775891341256367, 'pattern_counts': {'":': 10, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '643184', 'license': 'MIT', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2435, 'n_words': 601, 'path': 'automl.ipynb', 'repo_name': 'abhiojha8/Heart_Failure_Prediction_Azure', 'size': '8444'}