Saving the Titanic with Azure Automated Machine Learning
The sinking of the Titanic in 1912 was one of the deadliest maritime disasters in history, resulting in the loss of over 1,500 lives. In this famous dataset, information about the passengers is provided, including whether they survived or not. Using machine learning, the goal is to build a model that predicts which passengers survived based on attributes like age, gender, ticket fare, and cabin class.
While experienced data scientists could carefully engineer features and fine-tune algorithms to build an accurate survival prediction model, this can be a time-consuming process requiring significant expertise. What if there was an automated way to rapidly build high-quality machine learning models?
This is where Azure Automated Machine Learning (AutoML) comes to the rescue. AutoML uses the power of cloud computing to automatically try many combinations of algorithms, preprocessing steps, and hyperparameters to find the best-performing model for your data. Whether you prefer a code-first or low-code approach, AutoML makes it easy to quickly build ML models without being an expert.
In this article, we‘ll use the Azure Machine Learning Python SDK to build an AutoML classifier to predict survival on the Titanic. While some familiarity with Azure ML and Python is helpful, you don‘t need to be a data science expert to follow along. We‘ll cover:
- Connecting to your Azure ML workspace
- Loading and splitting the Titanic dataset
- Configuring the AutoML classification experiment
- Launching the AutoML run to find the best model
- Retrieving the best model for analysis and deployment
By the end, you‘ll see how AutoML enables you to build highly accurate models in a fraction of the time of traditional approaches. Let‘s get started!
Connecting to Your Azure ML Workspace
The first step is to connect to your Azure Machine Learning workspace. If you don‘t already have a workspace, you can quickly create one in the Azure portal. Be sure to select a region with AutoML capabilities.
With the workspace created, install the latest Azure ML Python SDK in your development environment:
!pip install --upgrade azureml-sdk[automl]
Then connect to the workspace using:
from azureml.core import Workspace
ws = Workspace.from_config()
print(f"Connected to Azure ML workspace {ws.name}")
This assumes you have a config.json file with the workspace information in the same directory. If not, you can explicitly pass your subscription ID and resource group.
Loading the Titanic Dataset
With the workspace connection established, the next step is to load the Titanic data. You can download the CSV file from Kaggle.
To make the dataset accessible to AutoML, we‘ll upload it to the default datastore in the workspace:
from azureml.core import Dataset
default_ds = ws.get_default_datastore()
if "titanic" not in ws.datasets:
default_ds.upload_files(
files=["titanic.csv"],
target_path="titanic-data",
overwrite=True,
)
tab_data = Dataset.Tabular.from_delimited_files(
path=(default_ds, "titanic-data/*.csv")
)
tab_data.register(ws, "titanic", create_new_version=True)
else:
tab_data = ws.datasets["titanic"]
train, test = tab_data.random_split(percentage=0.75, seed=123)
This code uploads titanic.csv to a titanic-data folder in the workspace‘s default datastore (if not already present). It then creates a tabular dataset from that CSV file and registers it with the workspace. Finally, it splits the dataset into train and test subsets using random 75% / 25% split.
Configuring AutoML
With the dataset prepared, it‘s time to configure the AutoML classification experiment using the AutoMLConfig class. Here we specify:
- The name of the experiment
- The task type (classification)
- The compute target to run the experiment on (in this case a pre-created GPU cluster called "gpu-cluster")
- The training data (the train dataset from the previous split)
- The name of the label column to predict ("Survived")
- The number of iterations (50)
- The primary metric to optimize (AUC weighted)
from azureml.core.compute import ComputeTarget
from azureml.train.automl import AutoMLConfig
compute_target = ComputeTarget(ws, "gpu-cluster")
automl_config = AutoMLConfig(
task="classification",
name="Titanic-AutoML-Experiment",
compute_target=compute_target,
training_data=train,
label_column_name="Survived",
iterations=50,
primary_metric="AUC_weighted"
)
There are many more parameters you can tune such as:
- caps on training time or the number of models
- whether to apply automatic featurization to preprocess the features
- algorithm types to exclude
- whether to perform cross-validation
- whether to enable early stopping if the score doesn‘t improve
However, the above is a good starting point. Feel free to experiment with the settings as you get more familiar with AutoML.
Running the AutoML Experiment
Now we‘re ready to launch the AutoML experiment to find the best model:
from azureml.core.experiment import Experiment
experiment = Experiment(ws, "Titanic-AutoML")
run = experiment.submit(automl_config)
This submits the AutoML classification experiment and returns a Run object. You can monitor the progress of the run in the Azure ML studio.
AutoML will now automatically cycle through many combinations of data preprocessing steps, algorithms, and hyperparameters in search of the most accurate model. This can take some time, depending on the size of your dataset, number of iterations specified, and compute resources.
Once complete, the run details will show the performance of the best model, visualizations of the metrics over time, and explanations of the preprocessing steps and algorithms used.
Retrieving the Best Model
To retrieve the best-performing model from the AutoML run programmatically:
best_run, best_model = run.get_output()
print(f"Best run ID: {best_run.id}")
print(f"Best model: {best_model}")
print(f"Best AUC_weighted: {best_run.get_metrics()[‘AUC_weighted‘]}")
This grabs the run and fitted model with the highest AUC_weighted score. You can inspect its properties, visualize feature importance, generate predictions on new data, and finally register the model to the workspace for deployment:
from azureml.core.model import Model
model_name = best_model.__class__.__name__
model = best_run.register_model(
model_name=model_name,
model_path=f"./{model_name}"
)
print(f"Registered {model_name} to workspace {ws.name}")
And voila! With just a few lines of code, you‘ve built a highly optimized classifier to predict survival on the Titanic. AUC is already likely to be well over 0.8 on test data, which would be near the top of the Kaggle leaderboard for this problem.
The Power of AutoML
This Titanic example illustrates the power of Azure AutoML to automate the time-consuming, iterative process of feature engineering, algorithm selection, and hyperparameter tuning. Instead of spending hours or days manually developing a model, you can use AutoML to build one with superior performance in a fraction of the time.
Some key benefits of AutoML:
- Rapid development of high-quality models without extensive data science experience
- Automated featurization and data guardrails to deal with missing values, one-hot encoding, etc.
- Automatic search through many algorithms including LightGBM, XGBoost, Logistic Regression, Random Forest, etc.
- Efficient tuning of hyperparameters using Bayesian optimization and smart early stopping
- Easy model registration and deployment to production using Azure ML pipelines
- ONNX support for fast inference and interoperability with other ML frameworks
- Automatically generated model explanations to see most important features
While experienced data scientists can still squeeze out extra performance by manually optimizing models, AutoML sets a high bar for baseline performance. It enables rapid prototyping and can make machine learning more accessible across the organization.
Tips and Considerations
While AutoML automates a lot of the work, there are still some important considerations:
- AutoML can‘t make up for low-quality input data. Garbage in, garbage out still applies. Cleaning and verifying your data is still important.
- Be mindful of leakage between training and test sets. It‘s still important to split the data in a way that reflects how the model will be used in production.
- AutoML is great for tabular data, but other approaches like Azure Cognitive Services are better for unstructured data like images, speech, text, etc.
- Behind the scenes, AutoML is running many training iterations. This consumes a lot of compute power, so be aware of costs on your Azure subscription.
- Carefully consider the business goals, acceptable accuracy and latency, and whether a simpler, more explainable model might be better than a complex one.
- Don‘t just treat AutoML as a black box. Look under the hood at the algorithms and preprocessing steps used to learn about new data science techniques.
Conclusion and Next Steps
In summary, Azure AutoML enables you to quickly build high-quality machine learning models without being an expert. Using the SDK, CLI or studio GUI you can automate the time-consuming process of feature engineering and model selection to achieve superior performance in a fraction of the time.
The Titanic survival prediction problem illustrates how you can use AutoML to automatically train, tune, explain and deploy models with just a few lines of code. This same process can be applied to many other scenarios like fraud detection, churn prediction, demand forecasting, and more.
I encourage you to try AutoML on your own datasets and see how much time it can save you. Experiment with the many configuration options, evaluate model explanations, and deploy your models as a web service for applications to consume.
While AutoML is powerful, remember that it‘s not a complete replacement for data science expertise. It‘s still important to understand your data, define your problem clearly, and interpret results. Think of AutoML as a productivity booster that lets you rapidly prototype solutions and frees up time for high-value tasks.
I hope this article has given you a taste of what‘s possible with Azure AutoML. For more information, check out the official documentation and stay tuned for more articles on AutoML best practices, demos, and case studies.
What are your experiences with AutoML? Let me know in the comments!