coefs = [est[-1].coef_ for est in cv_results[“estimator”]]
what is est[-1].coef before the for loop ? i’ve never seen somthing before a for loop
1 Like
I assume that est
is a scikit-learn Pipeline
made of a transformer followed by the linear model.
est[-1]
extract the fitted linear model and access to its coefficient.
Just adding that I found this very confusing too. It took me a while to figure out that what is happening is the following:
- Get list of pipelines from cv_results
- Loop over pipelines
- For each pipeline (it’s not an estimator, despite what the variable name implies!), retrieve the last item, corresponding to the estimator: the linear model (the other item is the polynomial features transformer)
- Get the array of coefficients
- The list comprehension stacks them all together, and later we stuff this into a DataFrame
It wasn’t trivial for me to unpack this, and I found it to be a significant distraction from the learning objectives for this notebook. It might be helpful to add some kind of explanatory note in the notebooks for future iterations of this MOOC.