Back to snippets

azureml_automl_classification_experiment_with_tabular_dataset.py

python

This code configures and submits an Automated ML classificat

15d ago39 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
azureml_automl_classification_experiment_with_tabular_dataset.py
1import logging
2from azureml.core import Workspace, Experiment
3from azureml.train.automl import AutoMLConfig
4from azureml.core.dataset import Dataset
5
6# 1. Connect to Azure ML Workspace
7# Note: Requires a 'config.json' file in the current directory or explicit parameters
8ws = Workspace.from_config()
9
10# 2. Prepare the dataset (Example using a public web CSV)
11data_url = "https://automlsamplenotebookdata.blob.core.windows.net/automl-sample-notebook-data/bankmarketing_train.csv"
12dataset = Dataset.Tabular.from_delimited_files(path=data_url)
13training_data, validation_data = dataset.random_split(percentage=0.8, seed=223)
14
15# 3. Configure the AutoML settings
16automl_settings = {
17    "iteration_timeout_minutes": 10,
18    "experiment_timeout_hours": 0.3,
19    "enable_early_stopping": True,
20    "primary_metric": 'AUC_weighted',
21    "featurization": 'auto',
22    "verbosity": logging.INFO,
23    "n_cross_validations": 5
24}
25
26automl_config = AutoMLConfig(task='classification',
27                             debug_log='automl_errors.log',
28                             training_data=training_data,
29                             label_column_name='y',
30                             **automl_settings)
31
32# 4. Submit the experiment
33experiment = Experiment(ws, "automl-client-quickstart")
34local_run = experiment.submit(automl_config, show_output=True)
35
36# 5. Retrieve the best model
37best_run, fitted_model = local_run.get_output()
38print(best_run)
39print(fitted_model)