<jupyter_start><jupyter_text>Copyright (c) Microsoft Corporation. Licensed under the MIT license.  Azure Machine Learning / AutoML IntegrationCapture results of AutoML experiments on the dataset and persist to Data Lake for reporting and analysis.* NOTE: To run this notebook, you must completed Step 7: Automate the Machine Learning Process with Azure Machine Learning and Deploy the Model to ACI in the Deployment Guide.  Library Imports<jupyter_code>import azureml.core
from azureml.core import Workspace, Datastore, Dataset, Run
from azureml.core.run import Run
from azureml.core.experiment import Experiment
from azureml.core.model import Model
from azureml.interpret import ExplanationClient
from pyspark.sql.functions import *
import pprint<jupyter_output><empty_output><jupyter_text>Read Model Metrics from Azure MLConnect to the Azure ML workspace and extract metrics from the AutoML run.<jupyter_code># connect to Azure ML
subscription_id = ''
workspace_name = ''
resource_group = ''

ws = Workspace(subscription_id = subscription_id, resource_group = resource_group, workspace_name = workspace_name)

#pp = pprint.PrettyPrinter()
#pp.pprint(ws.get_details())<jupyter_output><empty_output><jupyter_text>Pull Metrics from RunGather the following metrics:* AUC = AUC_weighted* Accuracy = accuracy* Precision = precision_score_weighted* Recall = recall_score_weighted* F1 = f1_score_weighted<jupyter_code># pull all metrics of best run
experiment_name = ''
run_id = ''

experiment = Experiment(workspace=ws, name=experiment_name)
fetched_run = Run(experiment, run_id)
metrics = fetched_run.get_metrics()

#pp = pprint.PrettyPrinter()
#pp.pprint(metrics)
# select relevant metrics
auc = metrics.get('AUC_weighted')
accuracy = metrics.get('accuracy')
precision = metrics.get('precision_score_weighted')
recall = metrics.get('recall_score_weighted')
f1 = metrics.get('f1_score_weighted')

# combine into single dataframe
metrics_df = sc.parallelize([['AUC', auc], ['Accuracy', accuracy], ['Precision', precision], ['Recall', recall], ['F1', f1]]).toDF(('Metric', 'Value'))

#display(metrics_df)<jupyter_output><empty_output><jupyter_text>Read Feature Importances from AutoML<jupyter_code>client = ExplanationClient.from_run(fetched_run)
engineered_explanations = client.download_model_explanation(raw=False)
features_dict = engineered_explanations.get_feature_importance_dict()
# save to list and convert numpy types to native
features_list = []

for key, value in features_dict.items():
    temp = [key.item(),value.item()]
    features_list.append(temp)

# save to dataframe
features_df = spark.createDataFrame(features_list, ['Feature', 'Value'])

#display(features_df)<jupyter_output><empty_output><jupyter_text>Save Results to Data LakePersist the model results to CSV files on the Data Lake for reporting.<jupyter_code>data_lake_account_name = ''
file_system_name = ''
metrics_df.coalesce(1).write.option('header', 'true').mode('overwrite').csv(f"abfss://{file_system_name}" + "@"+ f"{data_lake_account_name}.dfs.core.windows.net/reporting/model_metrics")
features_df.coalesce(1).write.option('header', 'true').mode('overwrite').csv(f"abfss://{file_system_name}" + "@"+ f"{data_lake_account_name}.dfs.core.windows.net/reporting/feature_importances")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11006384919428398, 'fraction_numerical': 0.002736394040741867, 'mean_word_length': 7.612565445026178, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '414613', 'license': 'MIT', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1015, 'n_words': 247, 'path': 'Analytics_Deployment/synapse-workspace/notebooks/Azure_Automated_ML/5 - Azure ML Integration.ipynb', 'repo_name': 'microsoft/Azure-Synapse-Solution-Accelerator-Financial-Analytics-Customer-Revenue-Growth-Factor', 'size': '3289'}