ClassifierChain + StackingClassifier : AttributeError: 'StackingClassifier' object has no attribute 'final_estimator_'
See original GitHub issueDescribe 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:
- Created 3 years ago
- Comments:8 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@snaraya7 In this issue, the
StackingClassifier
is placed in aClassifierChain
, which does not requireStackingClassifier.fit
to be called. The internals ofClassifierChain.fit
will cloneStackingClassifier
removing the fitted attributes anyways:https://github.com/scikit-learn/scikit-learn/blob/986d8f2524ad06b87c73e0e1f80dd49a4c9ca1e6/sklearn/multioutput.py#L554
‘final_estimator_’ exception is thrown when StackingClassifier.fit( ) method is not called before predict( ).