How to compare ML Models in Simple Steps

How to compare ML Models in Simple Steps

How to compare ML Models in Simple Steps

Whenever building a Machine Learning Model, it is always preferred and advised to compare different models for accuracy and which model works best in a given scenario.

compare

And to be honest, remembering all the ML algorithms and their working to develop a model is not so easy. That is were LAZYPREDICT LazyPredict Github , helps us solve this problem.

Lazy Predict helps build a lot of basic models without much code and helps understand which models work better without any parameter tuning. lazy

Ok, so let's see and understand how we can use Lazypredict to help solve this.

To get started first we need to install the lazypredict package/module

To install run the below command

pip install lazypredict

Now the ML problems can be divided into two parts, Classification, and Regression. So First let us see how we can use lazypredict to compare various classification model with accuracy.

lazypredict for classification problems:

Firstly we have to import the classification module from lazypredict and then train_test_split from sklearn to split the data into training data and testing data and dataset from sklearn

from lazypredict.Supervised import LazyClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

Load the Breast Cancer Dataset from sklearn

data = load_breast_cancer()

Choose your data and target

X = data.data
y= data.target

Split the data into a training set and testing set

X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=.5,random_state =123)

Create lazyclassifier model

clf = LazyClassifier(verbose=0,ignore_warnings=True, custom_metric=None)

Train the data on the model

models,predictions = clf.fit(X_train, X_test, y_train, y_test)

Now we can print the model to compare all ML algorithm accuracy

models

The output will be something like this

classification.PNG

Complete Code for classification problem

from lazypredict.Supervised import LazyClassifier
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

data = load_breast_cancer()
X = data.data
y= data.target

X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=.5,random_state =123)

clf = LazyClassifier(verbose=0,ignore_warnings=True, custom_metric=None)
models,predictions = clf.fit(X_train, X_test, y_train, y_test)

print(models)

lazypredict for Regression problems:

Now for the Regression Problem, we need to predict continuous values and there are a bunch of ML algorithms to do that, To compare each algorithm with their accuracy. Let us see how we can achieve this using LazyPredict

Import the lazyregressor module and train_test_split and dataset from sklearn

from lazypredict.Supervised import LazyRegressor
from sklearn import datasets
from sklearn.utils import shuffle
import numpy as np

Load the dataset

boston = datasets.load_boston()

Select the dependent and Independent Variable

X, y = shuffle(boston.data, boston.target, random_state=13)
X = X.astype(np.float32)

Next split the data into training set and testing set

offset = int(X.shape[0] * 0.9)
X_train, y_train = X[:offset], y[:offset]
X_test, y_test = X[offset:], y[offset:]

Create a lazyregressor model

reg = LazyRegressor(verbose=0,ignore_warnings=False, custom_metric=None )

Train the model on data

models,predictions = reg.fit(X_train, X_test, y_train, y_test)

Now, printing the model we get to compare various ML algorithm with their accuracy

model

The output will be similar as below

regressor.PNG

Entire code

from lazypredict.Supervised import LazyRegressor
from sklearn import datasets
from sklearn.utils import shuffle
import numpy as np
boston = datasets.load_boston()
X, y = shuffle(boston.data, boston.target, random_state=13)
X = X.astype(np.float32)
offset = int(X.shape[0] * 0.9)
X_train, y_train = X[:offset], y[:offset]
X_test, y_test = X[offset:], y[offset:]
reg = LazyRegressor(verbose=0,ignore_warnings=False, custom_metric=None )
models,predictions = reg.fit(X_train, X_test, y_train, y_test)

Hope this blog helps you understand how we can use lazypredict to compare the accuracy with various available ML algorithms hence saving a lot of time.