Learn Data Science from Data School 📊

Tuesday Tip #14: Ensemble your ML models for better performance 🎯

Published 10 months ago • 1 min read

Hi Reader,

Last week, I had an hour-long chat with my friend Ken Jee for his podcast. Starting tomorrow, you can watch the conversation on the Ken’s Nearest Neighbors YouTube channel!

👉 Tip #14: How to ensemble your ML models

When working on a Machine Learning problem, it’s always a good idea to try different types of models to see which one performs best.

However, you can also use a process called “ensembling” to combine multiple models. The goal is to produce a combined model, known as an ensemble, that performs better than any of the individual models.

The process for ensembling is simple:

  • For a regression problem, you calculate the average of the predictions made by the individual regressors and use that as your prediction.
  • For a classification problem, you can either average the predicted probabilities output by the classifiers, or you can let the classifiers vote on which class to predict.

The idea behind ensembling is that if you have a collection of individually imperfect models, the “one-off” errors made by each model are probably not going to be made by the rest of the models. Thus, the errors will be discarded (or at least reduced) when ensembling the models.

Ensembling in scikit-learn

Here’s a simple example in which I ensembled Logistic Regression and Random Forests using scikit-learn’s VotingClassifier:

Notice that the accuracy of the ensemble (0.725) is significantly better than the accuracy of either individual model. (Check out the full code here.)

Advice for ensembling

➡️ Ensembling is useful any time model accuracy (or another evaluation metric) is your highest priority. Keep in mind that the ensemble will be less interpretable than the individual models.

➡️ It’s ideal to include at least 3 models in the ensemble.

➡️ It’s important that all models you include are performing reasonably well on their own.

➡️ It’s best if the included models generate their predictions using different processes, since they will be likely to make different types of errors. (This is what makes Logistic Regression and Random Forests good candidates for ensembling!)

If you enjoyed this week’s tip, please forward it to a friend! Takes only a few seconds, and it really helps me out! 🙌

See you next Tuesday!

- Kevin

P.S. Gym rats vs data scientists

Did someone awesome forward you this email? Sign up here to receive data science tips every week!

Learn Data Science from Data School 📊

Kevin Markham

Join 25,000+ aspiring Data Scientists and receive Python & Data Science tips every Tuesday!

Read more from Learn Data Science from Data School 📊

Hi Reader, Do any of these sound like you? You're new to the pandas library and you want to learn the fundamentals You have some experience with pandas, but you want to fill in the gaps in your knowledge You want to learn the best practices for data analysis with pandas in 2024 If so, you should enroll in my FREE course (launching today!), pandas in 30 days. Why learn pandas? pandas is a powerful, open source Python library for data analysis, manipulation, and visualization. If you're working...

8 days ago • 1 min read

Hi Reader, There's a gift for you somewhere in this email... just look for the 🎁 emoji! Tip #39: Six quick Python tricks Here's what I'll cover below: Return the number of unique values Count values with Counter Better debugging with f-strings Return multiple values from a function Count while looping Create a dictionary with a comprehension Let's get started! 👇 1️⃣ Return the number of unique values Need to know the number of unique values in an iterable? Convert it to a set and check the...

15 days ago • 2 min read

Hi Reader, My goal with Tuesday Tips is to help you get better at Data Science every week. Is there anything that would make these tips even more helpful for you? Let me know! 💬 You can find past tips at (Yes, that’s a real URL!) Tip #38: Five ways to rename your DataFrame columns Let's say that we have a simple pandas DataFrame: I prefer to use dot notation to select pandas columns, but that won't work since the column names have spaces. Let's fix this! The most flexible method...

22 days ago • 1 min read
Share this post