Article complete

Complete Guide to Using AutoSklearn - Tool For Faster Machine Learning Implementations

In this article, we will see how to make use of autosklearn for classification and regression problems.

A multitasking man with multiple arms, juggling a phone, laptop, clock, clipboard, and briefcase, symbolizes efficiency.

Key takeaways

  • AutoSklearn automates the machine learning process, saving time for users dealing with large datasets.
  • The tool simplifies the selection of algorithms and hyperparameter tuning, enhancing user experience.
  • AutoSklearn learns from previous models on similar datasets, improving accuracy through automatic ensemble models.
  • Installation of AutoSklearn requires specific commands depending on the operating system, such as Linux or Google Colab.

Takeaways drafted with AI from the article text.

In this article, we will see how to make use of autosklearn for classification and regression problems. 

Installing the package

Before we understand how to build models with autosklearn we need to install the package in our working environment. To do this we can use the pip command if you have a Linux Operating system. 

pip3 install auto-sklearn

However, if you are making use of Colab you will need to install the following:

!sudo apt-get install build-essential swig
!curl https://raw.githubusercontent.com/automl/auto-sklearn/master/requirements.txt | xargs -n 1 -L 1 pip install
!pip install auto-sklearn

This will install the library and we can move to the next step. 

AutoSklearn for classification problems

Now that we have everything needed to start we can build a model using autosklearn on a classification type problem. For these types of problems, we need to configure the method called AutoSklearnClassifier. Let us first select the dataset and then proceed with the model. 

The dataset

I will use a simple wine quality dataset from the UCI repository. For using the same dataset you can download it here. Now let us load the dataset.

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import pandas as pd
from autosklearn.classification import AutoSklearnClassifier
wine= read_csv('https://raw.githubusercontent.com/sharmaroshan/Wine-Quality-Predictions/master/winequality-red.csv')
wine

Splitting the dataset

Now, let us split the dataset into training and test sets and also split the dataset into features and targets respectively. 

dataset = wine.values
ft, target = dataset[:, :-1], dataset[:, -1]
X_train, X_test, y_train, y_test = train_test_split(ft, target, test_size=0.2, random_state=1)

Building the classification model

Since we are using auto-sklearn, we need not specify the name of the algorithm or the parameters. These are done automatically for us and the final result is displayed. 

autosk = AutoSklearnClassifier(time_left_for_this_task=60*2) 
autosk.fit(X_train, Y_train)
print(autosk.sprint_statistics())

Time_left_for_this_task is the amount of time the user specifies for searching all the right models. I have allowed the search to take place for two minutes but you can choose any amount of time as you wish. 

Now we have the statistics of the model and the algorithms that were checked were 21. Let us now see the accuracy of the model. 

pred = autosk.predict(X_test)
print("Accuracy score", sklearn.metrics.accuracy_score(y_test, pred))

This is a good score since we have not scaled or pre-processed the data and we have allowed the model to run only for 2 minutes. Thus, we have built a classification model using autosklearn. 

Autosklearn for regression

We have already seen how autosklearn works for classification type of models. Next, let us implement this for a regression problem and check the results. 

The dataset

For this, I will use the built-in sklearn dataset called Boston housing dataset. Let us now load the dataset. The task here is to predict the price of houses in Boston using the features given.

from sklearn.datasets import load_boston
import pandas as pd
boston_data=load_boston()
features=pd.DataFrame(boston_data.data,columns=boston_data.feature_names)
target=pd.DataFrame(boston_data.target,columns=['TARGET'])
dataset=pd.concat([features,target],axis=1)

Splitting the dataset

Let us split this dataset into train and test data using the train_test_split function of sklearn.

xtrain,xtest,ytrain,ytest=train_test_split(features,target,test_size=0.2)

Model building

Just like we used the autosklearnclassifier for classification, we will be using autosklearnregressor for regression models. 

regressor=autosklearn.regression.AutoSklearnRegressor(time_left_for_this_task=60*5) 
regressor.fit(xtrain, ytrain)

Here I have given the time as 5 minutes to see the impact on the results. 

Now, let us see the statistics of the model along with the error rate. Since this is a regression problem we will use the mean absolute error as the metric. 

print(regressor.sprint_statistics())
pred= model.predict(xtest)
mae = mean_absolute_error(ytest, pred)
print("MAE:" ,mae)

This shows that the error is very less which means there is less loss and the model has performed very well. It also shows that the validation score is 0.86 which is good accuracy. As we see the model has searched 57 algorithms in the 5 minutes and has performed really well. 

Conclusion

In this article, we saw how to use autosklearn and build both classification and regression models without having to specify the name of the algorithm. We achieved good results in both of these models. AutoSklearn can be really useful in business analytics and research to build faster and better models. 

Published

Spotted an error? Tell our editors. Read our editorial standards and corrections.

Selling to AI teams? Put your brand in front of the 5M+ people who read AIM every month. Plan a campaign

More in this story

regression · 5 stories
  1. You are here: Complete Guide to Using AutoSklearn - Tool For Faster Machine Learning Implementations
  2. Hands-on Linear Regression Using Sklearn
  3. Getting Started With Deep Learning Using TensorFlow Keras
  4. Beginners Guide To Logistic Regression In Python
  5. 8 AI/ML Terms Explained for Beginners
About the author

Contributor

I am an aspiring data scientist with a passion for teaching. I am a computer science graduate from Dayananda Sagar Institute. I have experience in building models in deep learning and reinforcement learning. My goal is to use AI in the field of education to make learning meaningful for everyone.

All stories
Belamy · Daily

The day’s AI news, before your first meeting.