Embracing Open Source Machine Learning Series-Lazy Predict Library

Raj Kadiyala
2 min readJan 2, 2023

--

I recently bumped into lazypredict library on linkedin and decided to give it a try for myself, Most posts I have seen stop at print(models). To recap, if you go to this github link https://github.com/shankarpandala/lazypredict, you will see an example in the readme section which I have seen repeated in many posts. For convenience I have pasted it here

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)

Here is where it can get even more interesting if you rummage through the source code and docstrings. For example, if you decided from the above model summary you really like Perceptron model and want to save it or use it or see all the param settings. You can do that by adding a single line of code to above snippet as shown below


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)
#--- ADD THIS LINE -----
model_dict=clf.provide_models(X_train, X_test, y_train, y_test)
#-------
print(models)

By adding this line (shown below), you now have access to all the models in model_dict variable. From here, you can test a single model’s predictions, score it or save it to a file to be loaded and used later. See example below.

# test a model score
model_dict['LinearSVC'].score(X_test, y_test)

# save the model
import joblib
joblib.dump(model_dict['LinearSVC'], 'testsavemodel.joblib')

# ----- from saved file testsavemodel.joblib -----

loaded_model=joblib.load('testsavemodel.joblib')
loaded_model.score(X_test, y_test)

--

--

Raj Kadiyala
Raj Kadiyala

Written by Raj Kadiyala

ML Guy, AI Evangelist, Outdoor Enthusiast, Home Brewer, Home Coffee Roaster, Nitro Tap Fan

No responses yet