Skip to content

Stacking does not adhere to scikit-learn conventions #17821

Closed
@a-wozniakowski

Description

@a-wozniakowski

If I test either MLPClassifier or MLPRegressor with check_estimator, then there is no issue. However, these neural networks cause StackingClassifier and StackingRegressor to fail the estimator check.

Here is the simplest code with which I encounter the issue:

from sklearn.neural_network import MLPClassifier, MLPRegressor
from sklearn.ensemble import StackingClassifier, StackingRegressor
from sklearn.utils.estimator_checks import check_estimator

clf = StackingClassifier(estimators=[('mlp', MLPClassifier())],
                         final_estimator=MLPClassifier())
check_estimator(clf)

reg = StackingRegressor(estimators=[('mlp', MLPRegressor())],
                        final_estimator=MLPRegressor())
check_estimator(reg)

Note that the neural networks will cause an issue as either argument, so I used them as both the first and second stacking layers for simplicity.

Within check_estimator the issue arises when the test check_sample_weights_pandas_series tries to fit with sample weights:

try:
estimator.fit(X, y, sample_weight=weights)

Eventually this leads to a TypeError as fit gets an unexpected keyword sample_weight from

def _fit_single_estimator(estimator, X, y, sample_weight=None,
message_clsname=None, message=None):
"""Private function used to fit an estimator within a job."""
if sample_weight is not None:
try:
with _print_elapsed_time(message_clsname, message):
estimator.fit(X, y, sample_weight=sample_weight)

Any solutions?

Versions

Python dependencies:
          pip: 20.1.1
   setuptools: 47.3.1.post20200616
      sklearn: 0.23.1
        numpy: 1.17.5
        scipy: 1.4.1
       Cython: 0.29.20
       pandas: 1.0.5
   matplotlib: 3.2.1
       joblib: 0.15.1
threadpoolctl: 2.1.0

Built with OpenMP: True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions