Automated Machine Learning (AutoML) refers to techniques for automatically discovering well-performing models for predictive modeling tasks with very little user involvement.
Auto-Sklearn is an open-source library for performing AutoML in Python. It makes use of the popular Scikit-Learn machine learning library for data transforms and machine learning algorithms and uses a Bayesian Optimization search procedure to efficiently discover a top-performing model pipeline for a given dataset.
In this tutorial, you will discover how to use Auto-Sklearn for AutoML with Scikit-Learn machine learning algorithms in Python.
After completing this tutorial, you will know:
- Auto-Sklearn is an open-source library for AutoML with scikit-learn data preparation and machine learning models.
- How to use Auto-Sklearn to automatically discover top-performing models for classification tasks.
- How to use Auto-Sklearn to automatically discover top-performing models for regression tasks.
Let’s get started.
Tutorial Overview
This tutorial is divided into four parts; they are:
- AutoML With Auto-Sklearn
- Install and Using Auto-Sklearn
- Auto-Sklearn for Classification
- Auto-Sklearn for Regression
AutoML With Auto-Sklearn
Automated Machine Learning, or AutoML for short, is a process of discovering the best-performing pipeline of data transforms, model, and model configuration for a dataset.
AutoML often involves the use of sophisticated optimization algorithms, such as Bayesian Optimization, to efficiently navigate the space of possible models and model configurations and quickly discover what works well for a given predictive modeling task. It allows non-expert machine learning practitioners to quickly and easily discover what works well or even best for a given dataset with very little technical background or direct input.
Auto-Sklearn is an open-source Python library for AutoML using machine learning models from the scikit-learn machine learning library.
It was developed by Matthias Feurer, et al. and described in their 2015 paper titled “Efficient and Robust Automated Machine Learning.”
… we introduce a robust new AutoML system based on scikit-learn (using 15 classifiers, 14 feature preprocessing methods, and 4 data preprocessing methods, giving rise to a structured hypothesis space with 110 hyperparameters).
— Efficient and Robust Automated Machine Learning, 2015.
The benefit of Auto-Sklearn is that, in addition to discovering the data preparation and model that performs for a dataset, it also is able to learn from models that performed well on similar datasets and is able to automatically create an ensemble of top-performing models discovered as part of the optimization process.
This system, which we dub AUTO-SKLEARN, improves on existing AutoML methods by automatically taking into account past performance on similar datasets, and by constructing ensembles from the models evaluated during the optimization.
— Efficient and Robust Automated Machine Learning, 2015.
The authors provide a useful depiction of their system in the paper, provided below.
Install and Using Auto-Sklearn
The first step is to install the Auto-Sklearn library, which can be achieved using pip, as follows:
1 |
sudo pip install autosklearn |
Once installed, we can import the library and print the version number to confirm it was installed successfully:
1 2 3 |
# print autosklearn version import autosklearn print('autosklearn: %s' % autosklearn.__version__) |
Running the example prints the version number.
Your version number should be the same or higher.
1 |
autosklearn: 0.6.0 |
Using Auto-Sklearn is straightforward.
Depending on whether your prediction task is classification or regression, you create and configure an instance of the AutoSklearnClassifier or AutoSklearnRegressor class, fit it on your dataset, and that’s it. The resulting model can then be used to make predictions directly or saved to file (using pickle) for later use.
1 2 3 4 5 |
... # define search model = AutoSklearnClassifier() # perform the search model.fit(X_train, y_train) |
There are a ton of configuration options provided as arguments to the AutoSklearn class.
By default, the search will use a train-test split of your dataset during the search, and this default is recommended both for speed and simplicity.
Importantly, you should set the “n_jobs” argument to the number of cores in your system, e.g. 8 if you have 8 cores.
The optimization process will run for as long as you allow, measure in minutes. By default, it will run for one hour.
I recommend setting the “time_left_for_this_task” argument for the number of seconds you want the process to run. E.g. less than 5-10 minutes is probably plenty for many small predictive modeling tasks (sub 1,000 rows).
We will use 5 minutes (300 seconds) for the examples in this tutorial. We will also limit the time allocated to each model evaluation to 30 seconds via the “per_run_time_limit” argument. For example:
1 2 3 |
... # define search model = AutoSklearnClassifier(time_left_for_this_task=5*60, per_run_time_limit=30, n_jobs=8) |
You can limit the algorithms considered in the search, as well as the data transforms.
By default, the search will create an ensemble of top-performing models discovered as part of the search. Sometimes, this can lead to overfitting and can be disabled by setting the “ensemble_size” argument to 1 and “initial_configurations_via_metalearning” to 0.
1 2 3 |
... # define search model = AutoSklearnClassifier(ensemble_size=1, initial_configurations_via_metalearning=0) |
At the end of a run, the list of models can be accessed, as well as other details.
Perhaps the most useful feature is the sprint_statistics() function that summarizes the search and the performance of the final model.
1 2 3 |
... # summarize performance print(model.sprint_statistics()) |
Now that we are familiar with the Auto-Sklearn library, let’s look at some worked examples.
Auto-Sklearn for Classification
In this section, we will use Auto-Sklearn to discover a model for the sonar dataset.
The sonar dataset is a standard machine learning dataset comprised of 208 rows of data with 60 numerical input variables and a target variable with two class values, e.g. binary classification.
Using a test harness of repeated stratified 10-fold cross-validation with three repeats, a naive model can achieve an accuracy of about 53 percent. A top-performing model can achieve accuracy on this same test harness of about 88 percent. This provides the bounds of expected performance on this dataset.
The dataset involves predicting whether sonar returns indicate a rock or simulated mine.
No need to download the dataset; we will download it automatically as part of our worked examples.
The example below downloads the dataset and summarizes its shape.
1 2 3 4 5 6 7 8 9 |
# summarize the sonar dataset from pandas import read_csv # load dataset url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/sonar.csv' dataframe = read_csv(url, header=None) # split into input and output elements data = dataframe.values X, y = data[:, :-1], data[:, -1] print(X.shape, y.shape) |
Running the example downloads the dataset and splits it into input and output elements. As expected, we can see that there are 208 rows of data with 60 input variables.
1 |
(208, 60) (208,) |
We will use Auto-Sklearn to find a good model for the sonar dataset.
First, we will split the dataset into train and test sets and allow the process to find a good model on the training set, then later evaluate the performance of what was found on the holdout test set.
1 2 3 |
... # split into train and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1) |
The AutoSklearnClassifier is configured to run for 5 minutes with 8 cores and limit each model evaluation to 30 seconds.
1 2 3 |
... # define search model = AutoSklearnClassifier(time_left_for_this_task=5*60, per_run_time_limit=30, n_jobs=8) |
The search is then performed on the training dataset.
1 2 3 |
... # perform the search model.fit(X_train, y_train) |
Afterward, a summary of the search and best-performing model is reported.
1 2 3 |
... # summarize print(model.sprint_statistics()) |
Finally, we evaluate the performance of the model that was prepared on the holdout test dataset.
1 2 3 4 5 |
... # evaluate best model y_hat = model.predict(X_test) acc = accuracy_score(y_test, y_hat) print("Accuracy: %.3f" % acc) |
Tying this together, the complete example is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# example of auto-sklearn for the sonar classification dataset from pandas import read_csv from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder from sklearn.metrics import accuracy_score from autosklearn.classification import AutoSklearnClassifier # load dataset url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/sonar.csv' dataframe = read_csv(url, header=None) # print(dataframe.head()) # split into input and output elements data = dataframe.values X, y = data[:, :-1], data[:, -1] # minimally prepare dataset X = X.astype('float32') y = LabelEncoder().fit_transform(y.astype('str')) # split into train and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1) # define search model = AutoSklearnClassifier(time_left_for_this_task=5*60, per_run_time_limit=30, n_jobs=8) # perform the search model.fit(X_train, y_train) # summarize print(model.sprint_statistics()) # evaluate best model y_hat = model.predict(X_test) acc = accuracy_score(y_test, y_hat) print("Accuracy: %.3f" % acc) |
Running the example will take about five minutes, given the hard limit we imposed on the run.
Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.
At the end of the run, a summary is printed showing that 1,054 models were evaluated and the estimated performance of the final model was 91 percent.
1 2 3 4 5 6 7 8 9 |
auto-sklearn results: Dataset name: f4c282bd4b56d4db7e5f7fe1a6a8edeb Metric: accuracy Best validation score: 0.913043 Number of target algorithm runs: 1054 Number of successful target algorithm runs: 952 Number of crashed target algorithm runs: 94 Number of target algorithms that exceeded the time limit: 8 Number of target algorithms that exceeded the memory limit: 0 |
We then evaluate the model on the holdout dataset and see that classification accuracy of 81.2 percent was achieved, which is reasonably skillful.
1 |
Accuracy: 0.812 |
Auto-Sklearn for Regression
In this section, we will use Auto-Sklearn to discover a model for the auto insurance dataset.
The auto insurance dataset is a standard machine learning dataset comprised of 63 rows of data with one numerical input variable and a numerical target variable.
Using a test harness of repeated stratified 10-fold cross-validation with three repeats, a naive model can achieve a mean absolute error (MAE) of about 66. A top-performing model can achieve a MAE on this same test harness of about 28. This provides the bounds of expected performance on this dataset.
The dataset involves predicting the total amount in claims (thousands of Swedish Kronor) given the number of claims for different geographical regions.
- Auto Insurance Dataset (auto-insurance.csv)
- Auto Insurance Dataset Description (auto-insurance.names)
No need to download the dataset; we will download it automatically as part of our worked examples.
The example below downloads the dataset and summarizes its shape.
1 2 3 4 5 6 7 8 9 |
# summarize the auto insurance dataset from pandas import read_csv # load dataset url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv' dataframe = read_csv(url, header=None) # split into input and output elements data = dataframe.values X, y = data[:, :-1], data[:, -1] print(X.shape, y.shape) |
Running the example downloads the dataset and splits it into input and output elements. As expected, we can see that there are 63 rows of data with one input variable.
1 |
(63, 1) (63,) |
We will use Auto-Sklearn to find a good model for the auto insurance dataset.
We can use the same process as was used in the previous section, although we will use the AutoSklearnRegressor class instead of the AutoSklearnClassifier.
1 2 3 |
... # define search model = AutoSklearnRegressor(time_left_for_this_task=5*60, per_run_time_limit=30, n_jobs=8) |
By default, the regressor will optimize the R^2 metric.
In this case, we are interested in the mean absolute error, or MAE, which we can specify via the “metric” argument when calling the fit() function.
1 2 3 |
... # perform the search model.fit(X_train, y_train, metric=auto_mean_absolute_error) |
The complete example is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# example of auto-sklearn for the insurance regression dataset from pandas import read_csv from sklearn.model_selection import train_test_split from sklearn.metrics import mean_absolute_error from autosklearn.regression import AutoSklearnRegressor from autosklearn.metrics import mean_absolute_error as auto_mean_absolute_error # load dataset url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/auto-insurance.csv' dataframe = read_csv(url, header=None) # split into input and output elements data = dataframe.values data = data.astype('float32') X, y = data[:, :-1], data[:, -1] # split into train and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1) # define search model = AutoSklearnRegressor(time_left_for_this_task=5*60, per_run_time_limit=30, n_jobs=8) # perform the search model.fit(X_train, y_train, metric=auto_mean_absolute_error) # summarize print(model.sprint_statistics()) # evaluate best model y_hat = model.predict(X_test) mae = mean_absolute_error(y_test, y_hat) print("MAE: %.3f" % mae) |
Running the example will take about five minutes, given the hard limit we imposed on the run.
You might see some warning messages during the run and you can safely ignore them, such as:
1 |
Target Algorithm returned NaN or inf as quality. Algorithm run is treated as CRASHED, cost is set to 1.0 for quality scenarios. (Change value through "cost_for_crash"-option.) |
Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.
At the end of the run, a summary is printed showing that 1,759 models were evaluated and the estimated performance of the final model was a MAE of 29.
1 2 3 4 5 6 7 8 9 |
auto-sklearn results: Dataset name: ff51291d93f33237099d48c48ee0f9ad Metric: mean_absolute_error Best validation score: 29.911203 Number of target algorithm runs: 1759 Number of successful target algorithm runs: 1362 Number of crashed target algorithm runs: 394 Number of target algorithms that exceeded the time limit: 3 Number of target algorithms that exceeded the memory limit: 0 |
We then evaluate the model on the holdout dataset and see that a MAE of 26 was achieved, which is a great result.
1 |
MAE: 26.498 |
Further Reading
This section provides more resources on the topic if you are looking to go deeper.
- Automated machine learning, Wikipedia.
- Efficient and Robust Automated Machine Learning, 2015.
- Auto-Sklearn Homepage.
- Auto-Sklearn GitHub Project.
- Auto-Sklearn Manual.
Summary
In this tutorial, you discovered how to use Auto-Sklearn for AutoML with Scikit-Learn machine learning algorithms in Python.
Specifically, you learned:
- Auto-Sklearn is an open-source library for AutoML with scikit-learn data preparation and machine learning models.
- How to use Auto-Sklearn to automatically discover top-performing models for classification tasks.
- How to use Auto-Sklearn to automatically discover top-performing models for regression tasks.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Hello Jason,
I’ve tried to run Auto-Sklearn on Google CO-Lab without success.
It seems there is a conflict with the current version of Pandas (1.0.x) while the package runs on a prior version (0.25.x).
I’ll try to solve this issue.
Anyway, your explanations are always the best !
Regards,
Alessandro
Perhaps try running on your workstation.
I also had problem with Pandas >0.25.x. Downgrading to 0.25.3, substituting with the arff package with liac-arff fixed it.
Here are the versions I’m using if that helps at all:
how did you compute this list?
I printed the version of each library in turn with this script:
Is it possible to evaluate the automatically selected model by hand? I mean, is there a method to call for all the parameters and configuration set to the model at the end?
Yes, once defined you can evaluate it on your own test harness.
This is such informative information. Really very useful. I wanted to use this in my live project but could not move ahead.
This package does not seem to be supported by windows as per their official website. Neither could find a way out for Anaconda which I am using at present.
This command mentioned above does not work in Anaconda:
sudo pip install autosklearn
Kindly help how we can use it in Anaconda env.
You’re welcome.
Sorry to hear that. Perhaps try using conda to install the package?
TypeError: ‘generator’ object is not subscribable
error is occurred while running the classification problem
This may help:
https://machinelearningmastery.com/faq/single-faq/why-does-the-code-in-the-tutorial-not-work-for-me
I saw the same thing. According to this GitHub issues:
https://github.com/automl/auto-sklearn/issues/380
you should
pip install liac-arff
first remove arff:
pip uninstall arff
then
pip install liac-arff
Thanks for sharing.
Thank you for yet another great article Jason!
I got an error for the regression part:
—————————————————————————
TypeError Traceback (most recent call last)
in
17 model = AutoSklearnRegressor(time_left_for_this_task=60, per_run_time_limit=30, n_jobs=8)#, metric=auto_mean_absolute_error)
18 # perform the search
—> 19 model.fit(X_train, y_train, metric=auto_mean_absolute_error)
20 # summarize
21 print(model.sprint_statistics())
TypeError: fit() got an unexpected keyword argument ‘metric’
I think the metric part shall be moved to the model definition (see commmented part above).
[Keep rocking!]
Sorry to hear that, perhaps check that all of your libraries are up to date?
Hi!
I got the same error. And actually looking at the documentation “metric” is a parameter of AutoSklearnRegressor(), not fit().
Thanks for tutorial!
Nice work.
Thanks for the tutorial,
We are really keen to see some tutorials on Knowledge Graphs and Knowledge Graph Embeddings 🙂
Thanks,
Bill
Thanks Bill.
Looks like just what I need. Unfortunately, install was not successful. Will have to revisit when I have lots of time to troubleshoot.
Sorry to hear that.
Thank you for your great post.
Does Auto-Sklearn always got the better performance compared to the fine-tuned individual models?
No, but it can find a good model quickly.
Tq for the informative explanation…I like it much…
but I am interested to know what algorithm does python used in auto sklearn for ml? Does it perform as same as using weka..
How I want to know the algorithm for classification and regression that applicable in lib python?
Tqvm
You’re welcome.
It uses Bayesian Optimization to search for an appropriate algorithm.
Hi Jason,
I have windows10 laptop. When i tried installing auto-sklearn, it said it couldn’t becoz of system compatibility issues. Then i checked in git and got to know that we can’t install in windows machine. Am i right? Does it run only on unix machines?
Please help!!!
Regards,
Amit
Sorry, I don’t know about windows machines.
It is not possible to run auto-sklearn on a Windows machine.
https://automl.github.io/auto-sklearn/master/installation.html
Perhaps try a virtual machine?
Can we achive the parameters what was internally used in to get the best results.
Yes, the best model include the hyperparameters used.
How do you know what algo it has selected eg GBM or is it an ensemble
You can use model.show_models() to show the ensemble of models.
Hi Jason,
How to check which model is chosen thro AutoSklearn?
You can use the following to show the models in the final ensemble:
Please how to install auto-sklearn in anaconda windows 10 because auto-sklearn has the following system requirements:
Linux operating system (for example Ubuntu) (get Linux here),
Python (>=3.5) (get Python here).
C++ compiler (with C++11 supports) (get GCC here) and
SWIG (version 3.0.* is required; >=4.0.0 is not supported) (get SWIG here).
Perhaps use an AWS EC2 instance or a linux virtual machine.
For everyone facing issues installing auto sklearn on windows in Google Colab, should run on jupyter as well but have not tried out.Run the below codes and auto sklearn runs without issue
!apt-get install swig -y
!pip install Cython numpy
# sometimes you have to run the next command twice on colab
# I haven’t figured out why
!pip install auto-sklearn
Thanks for sharing.
Hi Jason,
Gettting this error when trying out the classifier for auto sklearn
ypeError: ‘generator’ object is not subscribable
error is occurred while running the classification problem
Kindly help if possible and thanks for all the great blogs.
Sorry, I have not seen this error. Perhaps try searching/posting on stackoverflow.
Hi Jason,
Thanks for writing this blog as there are very fews articles online covering auto-sklearn.
A question: does auto-sklearn really offer any “feature engineering” stuff? It look like feature preprocessors just do dimension reduction or compression. For example, if we have a datetime variable, can auto-sklearn automatically create hour, day, month, year engineered features out of this variable?
Not really. You could add those modules.
Could you provide an example of how to “add those modules”?
Thanks for the suggestion, perhaps in the future.
Hi Jason,
“Using a test harness of repeated stratified 10-fold cross-validation with three repeats, a naive model can achieve an accuracy of about 53 percent. A top-performing model can achieve accuracy on this same test harness of about 88 percent. This provides the bounds of expected performance on this dataset.”
Could you tell me what algorithms did you use to get the naive and top-performing models respectively?
You can see here:
https://machinelearningmastery.com/results-for-standard-classification-and-regression-machine-learning-datasets/
Hi Jason,
“At the end of the run, a summary is printed showing that 1,759 models were evaluated and the estimated performance of the final model was a MAE of 29.”
Is there any way to see what algorithms auto-sklearn uses to generate these 1,759 models? Does auto-sklearn include xgboost as one of the algorithms to build models?
Yes, as follows:
I don’t know if it supports xgboost off hand, sorry.
Does it make Cross validation to choose best model?
Good question, I’m not sure off the cuff.
Perhaps check the library documentaiton?
Hi Jason, Simplified and useful as usual. Thanks a lot.
I feel a bit frustrated because by using Automated ML I feel like no need no more to waste time diving into the different steps to preprocess data and testing different techniques to build a good model.
What do you think?
I think AutoML is great for a quick model or to get a quick idea of what works.
We can still get better/best results from hand crafted models. Case in point are ML competitions. As soon as competitions are consistently won by AutoML, it’s time to move up the stack.
Hi Jason, I tried the same code in google colab, but generate the error as below:
model.fit(X_train,y_train)
TypeError: __init__() got an unexpected keyword argument ‘local_directory’
Could you please help? Thanks!
This is a common question that I answer here:
https://machinelearningmastery.com/faq/single-faq/do-code-examples-run-on-google-colab
There are so many things how can we find it.
Hi Ketan…Please clarify your question so that we can better assist you.
Hello.This article was really fascinating, particularly since
I was looking for thoughts on this subject last Sunday.
Thanks!
Very interesting. I will try this out. Does it also perform some feature selection? And does it preprocess input data (normalization, categorical values – one hot encoding)? Or is all of that still part of manual preprocessing?
Thanks.
Good question, I believe it does involve selecting some data prep.
I’d recommend double checking the documentation.
Hello ,
I am trying to run the example for the AutoSklearn for classification example using the sonar.csv dataset and each time I have this error : EOFError : unexpected EOF.
~/anaconda3/lib/python3.8/multiprocessing/forkserver.py in read_signed(fd)
332 s = os.read(fd, length – len(data))
333 if not s:
–> 334 raise EOFError(‘unexpected EOF’)
335 data += s
336 return SIGNED_STRUCT.unpack(data)[0]
EOFError: unexpected EOF
I am using autosklearn : 0.12.3 and I have tried all the example from the AutoSklearn and they work well. So I was wondering if you have encountered that problem and if so how did you solve it.
thank you
Sorry, I have not seen this problem.
Perhaps try posting code/error on stackoverflow or an issue on the autosklaern project itself?
Hi Jason, I’m using the Auto-Sklearn for the classification task, and it runs well,
however, it doesn’t offer too many visualization examples,
I was wondering if I can import wandb to deal with such a task?
https://wandb.ai/lavanyashukla/visualize-sklearn/reports/Visualize-Scikit-Models–Vmlldzo0ODIzNg
If not, is it any other way that can show better plots?
Thank you
Perhaps once the search finds a final model you can re-fit it and visualize it.
Hi Jason,
thank you for the post. It seems very interested.!
While trying to install autosklearn on my Mac with python 3.6 (installed following your post:
https://machinelearningmastery.com/install-python-3-environment-mac-os-x-machine-learning-deep-learning/)
with your command
% sudo pip install autosklearn
I got he following error:
ERROR: Could not find a version that satisfies the requirement autosklearn (from versions: none)
ERROR: No matching distribution found for autosklearn
I tried other options, following autosklearn suggestions :
% curl https://raw.githubusercontent.com/automl/auto-sklearn/master/requirements.txt | xargs -n 1 -L 1 pip3 install
% sudo pip3 install -U auto-sklearn
but also It does not work 🙁
any suggestion?
thanks
Ouch!?
Sorry to hear it, perhaps the lib has not been updated recently to keep track of sklearn.
Perhaps a new env is required with some versions (maybe sklearn) rolled back?
Hi jason,
Thanks for the wonderful article.Learnt a lot, however one doubt I have in mind:
For a binary classification , does auto sklearn classifier takes the probability threshold of 0.5 by default? Can it be changed?
It can be changed of course. That is the predict_proba() function of the classifier. However, 0.5 threshold makes mathematical sense because inverting the result give exact opposite for binary classification. That’s why it is the default.
Hello,
Thank you for your post.
However, how can we report what is the selected model and its parameters? Also, can we select the metric we want to use for evaluation in the search process?
Thank you.
Hi Mary…The following is a great discussion of this concept:
https://github.com/automl/auto-sklearn/issues/872
Hello,
Thank you for your post.
However, how can we report what is the selected model and its parameters? Also, can we select the metric we want to use for evaluation in the search process?
Thank you.
Great article man!
Can you give some tips on how to avoid overfitting?
Hi Sushant…The following resource may be of interest to you:
https://machinelearningmastery.com/introduction-to-regularization-to-reduce-overfitting-and-improve-generalization-error/