Way to go! Now that we are getting the hang of pipelines, let’s take things up a notch and now search over a range of different types of models, all of which have their own sets of hyperparameters. In the original pipeline, we defined regr to be an instance of LinearRegression. Then in defining the parameter grid to search over, we used the dictionary {"regr__fit_intercept": [True,False]} to define the values of the fit_intercept term. We can equivalently do this by passing both the estimator AND parameters in a dictionary as

{'regr': [LinearRegression()], "regr__fit_intercept": [True,False]}

There are two main ways we can access elements of this pipeline:

  1. We can access the list of steps by using .steps and accessing the elements of the tuple. This of the same form as how the pipeline was initially defined. For examples, to access the last step of best_model use best_model.steps[-1]. This will return a tuple with the name and estimator. To access the estimator, use best_model.steps[-1][-1].
  2. Another way the steps can be accessed is by name – after all, we gave each steps a string name when we defined them. The same regression model can be access using best_model.named_steps['regr'] to access the named step regr.



Update the search_space dictionary to include values for alpha in lasso and ridge regression models. Use np.logspace(-4,2,10).


Fit the GridSearchCV on the training data and print the best estimator and score from the search.


Access the regression model from the best_model pipeline. What type is it and what are the hyperparameters? Access the parameters with .get_params().


For a challenge, access the hyperparameters of the categorical preprocessing step.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?