M07 Final Quiz Q3

Hi,
I do not manage to get the weights (_coef ?) to inspect the weights of the linear models fitted during cross-validation ;

linear_model = make_pipeline(StandardScaler(), RidgeCV())

cv = ShuffleSplit(n_splits=4, random_state=0)

cv_results_linear_model = cross_validate(
    linear_model, data_linear_model, target, cv=cv,
    scoring="neg_mean_absolute_error",
    return_estimator=True, return_train_score=True)

for estimator in cv_results_linear_model["estimator"]:
    print(estimator.coefs_)

I get the following error : “AttributeError: ‘Pipeline’ object has no attribute ‘coef_’”.
So obviously I should not go for the pipeline but I tried many options without success

You need to access the last step of the pipeline instead of trying to access coef_ from the pipeline:

for estimator in cv_results_linear_model["estimator"]:
    print(estimator[-1].coefs_)

Only the RidgeCV has the attribute coef_

I was lost as I guessed that I needed to “access” RidgeCV but didn’t find how…
As it was the last open question of the last quiz, thanks a lot for the great support all the way ; enabling me to finish the training.

Why is [-1] the right value to point on RidgeCV() ?
[1] seems also to work

ps: just removed the “s” →

for model in cv_results_linear_model["estimator"]:
    print(model[-1].coef_)

It is a Python feature. You can use negative indexing to index from right to left instead of positive indexing that goes from left to right. Since the predictor is always the most right estimator in our pipeline, using [-1] is therefore super handy.

1 Like

Is ascending the order of the print?

I mean, in the print we’ll get something like [B1, B2, B3, B4]