question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ClassifierChain + StackingClassifier : AttributeError: 'StackingClassifier' object has no attribute 'final_estimator_'

See original GitHub issue

Describe the bug

Getting an exception when chaining together StackingClassifiers using ClassifierChain.

Steps/Code to Reproduce

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_multilabel_classification
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
from sklearn.multioutput import ClassifierChain

X, y = make_multilabel_classification(n_classes=3, n_labels=3, random_state=0)
rfc = RandomForestClassifier(random_state=0, n_estimators=1, max_depth=1)
lr = LogisticRegression(random_state=0)
stacking_classifier = StackingClassifier([('rfc', rfc), ('lr', lr)])
classifier_chain = ClassifierChain(stacking_classifier)
classifier_chain = classifier_chain.fit(X, y)
proba = classifier_chain.predict_proba(X)
print(proba)

Expected Results

No error is thrown. Predicted probabilities for all classes of all outputs returned from clf.predict_proba(X)

Actual Results

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1477, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/vq/workspace/tennis-live-win-prob-all/src/bug_report.py", line 14, in <module>
    predictions = clf.predict_proba(X)
  File "/Users/vq/workspace/tennis-live-win-prob-all/venv/lib/python3.7/site-packages/sklearn/utils/metaestimators.py", line 114, in __get__
    getattr(delegate, self.attribute_name)
  File "/Users/vq/workspace/tennis-live-win-prob-all/venv/lib/python3.7/site-packages/sklearn/utils/metaestimators.py", line 117, in __get__
    attrgetter(self.delegate_names[-1])(obj)
AttributeError: 'StackingClassifier' object has no attribute 'final_estimator_'

Versions

System:
    python: 3.7.6 (default, Dec 30 2019, 19:38:28)  [Clang 11.0.0 (clang-1100.0.33.16)]
executable: /Users/vq/workspace/tennis-live-win-prob-all/venv/bin/python
   machine: Darwin-19.6.0-x86_64-i386-64bit
Python dependencies:
          pip: 21.0.1
   setuptools: 52.0.0
      sklearn: 0.24.1
        numpy: 1.20.0
        scipy: 1.6.0
       Cython: None
       pandas: 1.2.1
   matplotlib: 3.3.4
       joblib: 1.0.0
threadpoolctl: 2.1.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasjpfancommented, Aug 29, 2021

@snaraya7 In this issue, the StackingClassifier is placed in a ClassifierChain, which does not require StackingClassifier.fit to be called. The internals of ClassifierChain.fit will clone StackingClassifier removing the fitted attributes anyways:

https://github.com/scikit-learn/scikit-learn/blob/986d8f2524ad06b87c73e0e1f80dd49a4c9ca1e6/sklearn/multioutput.py#L554

1reaction
snaraya7commented, Aug 29, 2021

‘final_estimator_’ exception is thrown when StackingClassifier.fit( ) method is not called before predict( ).

Read more comments on GitHub >

github_iconTop Results From Across the Web

tensorflow - Stacking Classifier doesn't recognize Keras
This problem is because of the similar issue reported here for VotingClassifier . The solution is just adding this ...
Read more >
scikit-learn user guide
1.2.22 Why does Scikit-learn not directly work with, for example, pan- das.DataFrame? The homogeneous NumPy and SciPy data objects currently ...
Read more >
Scikit Learn Docs PDF - Scribd
1.2.22 Why does Scikit-learn not directly work with, for example, pan- das.DataFrame? The homogeneous NumPy and SciPy data objects currently expected are ...
Read more >
sklearn.multioutput.MultiOutputRegressor Example
`None` applies no normalization, "quantile" applies quantile normalization, and "standard" transforms data to have zero mean and unit variance.
Read more >
sklearn - Notebooks
It needs to be a classifier or a regressor when using StackingClassifier or ... can be used along with any estimator that has...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found