avatarRahul Pandey

Summary

This article introduces a quick way to compare multiple machine learning algorithms using the lazy predict library in Python for both classification and regression tasks.

Abstract

The article discusses the importance of finding the best performing machine learning algorithm for a given dataset, which is similar to the process of exploratory data analysis (EDA). It mentions the time-consuming process of manually testing multiple algorithms and introduces the lazy predict library as a solution. The author demonstrates the use of lazy predict on the iris dataset for classification algorithms and the Boston housing dataset for regression algorithms. The library provides a quick way to compare multiple machine learning algorithms with just a few lines of code, making it an efficient tool for model selection.

Bullet points

  • The article discusses the process of finding the best machine learning algorithm for a given dataset.
  • The traditional method of testing multiple algorithms can be time-consuming and requires multiple lines of code.
  • The lazy predict library offers a quick solution to compare machine learning algorithms without writing much code.
  • The author demonstrates the use of lazy predict on the iris dataset for classification algorithms and the Boston housing dataset for regression algorithms.
  • The library returns a pandas dataframe that contains important evaluation metrics and the time taken to execute the algorithm.
  • Lazy predict offers a quick way of comparing multiple machine learning algorithms with just a few lines of code.

START GUIDE

How to find the best performing Machine Learning algorithm

This article shows quick ways of comparing multiple machine learning algorithms for classification or regression.

P.S. I made this banner

Imagine a situation where you want to test if the given dataset has sufficient features to train machine learning algorithms or to test different algorithms’ performance on the given dataset. Both cases are pretty common in the field of data science.

Usually, to test the features, one can train models with no regularization and verify if the loss function is close to zero or not. This test can quickly tell if the model has enough parameters to memorize the dataset or not.

Which algorithm to use?

The answer to the question is similar to the process of Exploratory Data Analysis (EDA). Performing exploratory data analysis gives insight into the dataset. Similarly, there are several ways to find the best algorithm that works well for the dataset, but this often requires multiple lines of codes in a loop that can be time-consuming. However, to overcome this limitation, a python library called lazypredict can be used as a starting point for selecting the best performing algorithms.

This article shows an example comparing classification algorithms using models in a loop and lazypredict on the iris dataset. Also, it shows the comparison of different regression algorithms using lazypredict on the Boston housing dataset.

Classification algorithms comparison on the iris dataset

Before jumping into algorithm comparison, let’s talk about the data set. The iris dataset consists of 3 classes (Setosa, Versicolor, Virginica) with 50 samples of each iris plant.

Figure showing Iris setosa, Iris versicolor, and Iris virginica (Sources: 1, 2, 3)

The distinguishing features among the classes are sepal length, sepal width, petal length, and petal width. The figure below shows the relationship between features based on classes.

Pairplot to show the relationship between features. Target refers to classes of the iris plant, 0: ‘Setosa’ 1: ‘Versicolor’ 2: ‘Virginica.’

The code snippet below shows the steps for data exploration and feature scaling.

Classifiers in for loop:

First, let’s start with sklearn. This case shows running tests with 5 classifiers, such as Logistic regression, KNN, SVC, Random forest, and GaussianNB. The figure shows the visual representation of the code.

The figure shows the working principle of model comparison through looping

The code snippet below shows the steps for testing different models.

The figure below shows the evaluation metrics of different models.

Classification report of classifiers under test

From the result above, one can conclude that input features are sufficient to predict the classes, and all models are performing exceptionally well (maybe some overfitting issues). Pick any 2 or 3 models for further study, perform validation techniques to check the performance, and use regularization techniques in case of overfitting.

Lazypredict:

Lazy predict offers a quick solution to compare machine learning algorithms without writing much code. The code snippet below shows how to use lazypredict.

In the case of looping through algorithms shown in the previous section, it returns each algorithm’s evaluation metrics. Hence, an extra line of code is required to transform this into a pandas dataframe. However, lazypredict does everything in few lines of code. It returns a pandas dataframe that contains important evaluation metrics and the time taken to execute the algorithm. The figure below shows the top 5 out of 25 classifiers returned from the lazypredict.

Classification report of classifiers under test

Regression algorithms comparison on the Boston housing dataset

The previous section shows how lazypredict can speed up the model exploration process. This section shows the comparison of regression models using the lazypredict on the Boston housing dataset. The dataset contains 506 examples with 14 columns.

Figure showing attributes of Boston housing dataset in the background

The code snippet below shows the steps for data exploration and feature scaling.

The figure below shows the heatmap of the correlation between features.

Correlation between features.

After removing highly correlated features, the code snippet below shows how to use lazypredict for comparing regression algorithms.

The figure below shows the top 5 out of 41 regressors returned from the lazypredict.

Report of regressors under test

That’s it.

Lazypredict offers a quick way of comparing multiple machine learning algorithms with just a few lines of code.

I hope you enjoyed this article. Any questions? Have I missed something? Please reach out on my LinkedIn or Twitter.

Follow this Github link to access all the resources used for this article.

Cheers!

Rahul

Machine Learning
Data Science
Artificial Intelligence
Supervised Learning
Model Comparison
Recommended from ReadMedium