Finding the Best Machine Learning Model Quickly with Lazy Predict
One of the most time-consuming parts of any machine learning project is iterating through different models to find the one that performs best on your dataset and problem. Even experienced data scientists can spend days or weeks coding different algorithms, tuning hyperparameters, and comparing results to find the optimal approach.
But what if there was a tool that could automate this process and identify the best models for your data in just a few lines of code? Enter Lazy Predict, a Python library that does just that.
What is Lazy Predict?
Lazy Predict is an open-source Python library that automatically trains and evaluates multiple standard machine learning algorithms on a given dataset. Simply load your data, pass it to Lazy Predict, and within seconds you‘ll have a ranked list of which models perform best.
Lazy Predict covers all the standard supervised learning models from the popular scikit-learn library, including:
- Linear models like Logistic Regression and Linear SVC
- Decision trees like Random Forest and Extra Trees
- Naïve Bayes classifiers like Gaussian NB and Bernoulli NB
- Ensemble models like AdaBoost and Gradient Boosting
- Nearest neighbor models like K Neighbors and Radius Neighbors
- SVMs with different kernels like Linear, RBF, and Polynomial
For the full list of 30+ included classification algorithms and 40+ regression algorithms, check out the Lazy Predict documentation.
How does Lazy Predict work?
Using Lazy Predict is extremely straightforward. After installing the library, you simply load your data into X and y arrays, create a Lazy Predict classifier or regressor object, and call the fit method:
from lazypredict.Supervised import LazyClassifier
clf = LazyClassifier(verbose=0,
ignore_warnings=True,
custom_metric=None)
models, predictions = clf.fit(X_train, X_test, y_train, y_test)
Behind the scenes, Lazy Predict then trains and cross-validates every included model on your data and returns a pandas dataframe showing the performance metrics for each one. For classifiers, it returns statistics like:
- Accuracy
- Balanced accuracy
- ROC AUC
- F1 score
- Time taken to train
While for regressors, metrics include:
- R-squared
- Mean squared error
- Mean absolute error
- Time taken to train
The beauty of Lazy Predict is that with just a few lines of code, you instantly get a clear picture of which models are performing well on your dataset and which ones likely aren‘t worth pursuing further. You can then focus your efforts on tuning and optimizing the top few models rather than needing to experiment with every possibility.
Benchmarks and Performance
Of course, training dozens of models isn‘t a free lunch in terms of computational costs. The time required for Lazy Predict to run will scale with the size and complexity of your dataset.
To give you a sense of what to expect, here are some benchmarks I ran on my Intel Core i7 CPU for various classification datasets:
| Dataset | Samples | Features | Time |
|---|---|---|---|
| Iris | 150 | 4 | 4.1 sec |
| Wine | 178 | 13 | 7.3 sec |
| Breast Cancer | 569 | 30 | 13.4 sec |
| Digits | 1797 | 64 | 51.2 sec |
So while Lazy Predict makes model selection much faster in terms of human time, you‘ll still want a decently powerful machine to run it, especially as you get into the tens or hundreds of thousands of rows and columns. Using a cloud computing platform like Google Colab or AWS can help speed things up significantly.
Strategies for Selecting Models
Once you have your Lazy Predict results, how do you go about choosing which models to focus on tuning and deploying? Here are a few strategies I‘ve found effective:
-
Look at the top 3-5 performing models and assess them in terms of not just pure accuracy metrics but also training time and model explainability needs. Sometimes a simpler model that‘s almost as accurate as a complex one is the better business choice.
-
If multiple models are clustered closely in performance, select a diverse set that rely on different algorithmic approaches. For example, if Gradient Boosting, Random Forest, and Logistic Regression are all performing similarly, it‘s worth tuning instances of each to see which holds up best.
-
Do a cost-benefit analysis on the potential upside of tuning a model versus the time and resources required to do so. A 0.01 increase in F1 score might not be worth tens of hours trying to eke it out.
-
Consider the specific needs and constraints of your use case. If model inference needs to happen in real-time, lean towards simpler models that predict faster. If your dataset will be frequently updated, choose algorithms that can handle data drift gracefully.
Comparing to Other AutoML Libraries
Lazy Predict is far from the only library aiming to automate machine learning model selection. Other popular AutoML options include:
- TPOT: Uses genetic programming to optimize a scikit-learn pipeline for your data
- Auto-sklearn: An AutoML system built around scikit-learn
- H20 AutoML: Finds the best model and tunes it via random search and stacked ensembles
- Google Cloud AutoML: A managed service that automatically builds and deploys state-of-the-art models
In my experience, Lazy Predict stands out for its simplicity and speed in surfacing all your viable model options in just a few lines of code. It‘s the purest "plug and play" AutoML option out there.
That said, many of the other AutoML libraries offer additional functionality beyond just model selection, like automated hyperparameter tuning, ensembling of models, and even neural architecture search. If you‘re looking for an end-to-end AutoML solution, options like H20 or Google Cloud may be worth exploring.
Best Practices for Using Lazy Predict
To get the most value out of Lazy Predict, I recommend keeping these best practices in mind:
-
Use it to quickly identify promising models, not select a final model. Lazy Predict is designed to surface your best options, but all model scores should be taken with a grain of salt until you‘ve properly tuned them.
-
Don‘t skip important data preparation steps like cleaning, normalization, and handling missing values. Lazy Predict will perform better on an already curated dataset.
-
Be judicious in your use of computing resources. Running Lazy Predict on a huge dataset can be prohibitively expensive. If possible, run it on a representative sample of your data first before scaling up.
-
Pair it with other Python libraries for a more complete machine learning workflow. Tools like Pandas for data wrangling, Hyperopt for hyperparameter tuning, and MLflow for experiment tracking can help fill in the gaps in functionality.
-
Don‘t forget the importance of human intuition and domain expertise. While Lazy Predict is great at surfacing strong candidate models, it‘s up to you to assess them holistically and determine which will deliver real business value. Machine learning is both an art and a science.
The Impact on Data Science
As AutoML tools like Lazy Predict gain popularity, it‘s worth reflecting on how they might impact the day-to-day role of machine learning practitioners. Will they make data scientists obsolete?
I firmly believe the answer is no. While AutoML can automate some of the most repetitive parts of a data scientist‘s job, it is far from a complete replacement for human expertise.
At the end of the day, tools like Lazy Predict are just that – tools. They are force multipliers that can help data scientists work smarter and faster. But tasks like thoroughly understanding business needs, cleaning data, conducting error analysis, and communicating results still require the human touch. Plus, any good data scientist knows the model is only as good as the data and assumptions that go into it.
In my view, AutoML will allow data scientists to focus more of their time on the "bigger picture" parts of the job that can‘t be automated away – things like problem framing, project scoping, and stakeholder management. Technical skills will still be important, but greater AutoML adoption may mean an increased emphasis on soft skills as key differentiators.
Conclusion
Lazy Predict is an incredibly powerful tool to have in your data science toolkit. By automating one of the most time-consuming aspects of machine learning – model selection – it allows you to quickly hone in on the most promising approaches and start delivering value faster.
That said, it‘s not a silver bullet. Lazy Predict should be used to augment human expertise, not replace it. And like any tool, it must be wielded properly and appropriately to be effective.
If you‘re a data scientist or ML engineer looking to up your game, I highly recommend adding Lazy Predict to your workflow. It may just change the way you approach model selection and development.