'Pipeline' object has no attribute 'n_iter_'

Hi all,

In the module 1, secction Preprocessing for numerical features

model_name = model.__class__.__name__
score = model.score(data_test, target_test)
print(f"The accuracy using a {model_name} is {score:.3f} "
      f"with a fitting time of {elapsed_time:.3f} seconds "
      f"in {model.n_iter_[0]} iterations")

I got this error:

AttributeError                            Traceback (most recent call last)
<ipython-input-17-0cdc76cc0bd7> in <module>
      3 print(f"The accuracy using a {model_name} is {score:.3f} "
      4       f"with a fitting time of {elapsed_time:.3f} seconds "
----> 5       f"in {model.n_iter_[0]} iterations")

AttributeError: 'Pipeline' object has no attribute 'n_iter_'

both on MOOC and Colab. Any clue would be appreciated.

n_iter_ is an attribute of the LogisticRegression. When you are using in a pipeline, you need to do model[-1].n_iter_.

If you get the error with some of the notebook online, it is possible that you executed the code not in a sequential manner and we expect a logistic regression classifier to execute the cell but model is indeed a pipeline.

1 Like