How To Include An ADA Boost Model’s Base Estimator In A Grid Search When It’s Contained In A Pipeline (*GASP*)

Easy there friend. I promise, there’s a very easy way we will get through this.

Pipelines and Grid Searches

As any machine learning specialist will tell you, pipelines and grid searches are of the utmost importance for optimizing and tuning the parameters of your machine learning model. Pipelines ensure that your data is going through the appropriate steps in the correct order (cleaning, pre-processing, vectorizing, classifying, etc). Here’s an example:

In the above example, you can see that any data that I would fit to the example_pipeline would FIRST be vectorized through the TF-IDF Vectorizer (obviously, we’re working with language here), SECONDLY, SMOTE would be run on the vectorized language which would auto balance the minority class by artificially creating additional data points that are variations on existing points in the minority class, and FINALLY, that data is fed into the Decision Tree Classifier which classifies the data based on the target I have asked it to look at.

Grid Searches are used to test multiple variations of a model to find the ideal parameters for what you want your model to do. Running a grid search on a model by itself is fairly straight forward, here’s an example:

You can see above that grid searches are fairly intuitive. Pipelines have multiple instantiated classes so how do we specify specific parameters for each class? Let’s use the example Pipeline we instantiated earlier.

You can see above that we can simply reference each instantiation by its defined name that we gave it in example_pipeline followed by a double underscore __ and then the parameters. Easy peasy.

Enter The ADA Boost Model

ADA Boost Models are incredibly powerful models that uses multiple weak learners which feeds data of what was incorrectly classified to the next weak learner and creates stronger learners through this process (this is a bit of an oversimplification but this post is meant for people who are already familiar with ADA Boost). By default, the ADA Boost Model uses a Decision Tree with a max depth of 1 as its weak learner. There are a number of parameters that can be grid searches in an ADA Boost Model but the Decision Tree as a base estimator is one of those parameters:

You are welcome to change the base estimator based on your needs but for our case, we want to use a decision tree which is already nested inside the ADA Boost model. So how do we access the Decision Tree parameters in a grid search? You can code a complicated way to do so (with additional variables and for loops) OR you can use this easy approach:

In the above parameters, n_estimators and learning_rate are parameters specifically for the ADA Boost model BUT the other parameters are for the Decision Tree Model. We are accessing the base estimator by first specifying the “ada” name in the pipeline followed by a double underscore __ and THEN we reference the base_estimator (which we already defined in the pipeline inside the ADABoostClassifier’s parameters) followed by ANOTHER double underscore __ and then simply reference the parameters of the decision tree classifier that we would like to tune. Boom. Mic Drop.

This is a great way to really dig deeper into your grid searches and further fine tune your models when base estimators are part of the journey (or really to reference anything further in a pipeline). Doing so will most certainly make your Grid Searches longer (obviously but don’t forget to make your n_jobs = -1) but will test out more possibilities and open up your odds of finding a better model. Maybe one that will change the world/your company/just you. Good luck machine learners!

Chris de la Cruz is a guacamole-eating and fitness-loving data scientist, actor, freestyler, and beatboxer (under the moniker MC Lightbulb)