Thanks to visit codestin.com
Credit goes to github.com

Skip to content

_check_feature_names raises UserWarning when accessing bagged estimators #21599

Open
@ArturoAmorQ

Description

@ArturoAmorQ

Describe the bug

Using the estimators inside a BaggingRegressor to predict data raises a

UserWarning: X has feature names, but DecisionTreeRegressor was fitted without feature names

coming from _check_feature_names, even if the BaggingRegressor or its base estimator (in this case DecisionTreeRegressor) are able to take the feature names into account while fitting.

Steps/Code to Reproduce

from sklearn.ensemble import BaggingRegressor
import pandas as pd
import numpy as np

df = pd.DataFrame(
    {
        "feature_name": [-12.32, 1.43, 30.01, 22.17],
        "target": [72, 55, 32, 43],
    }
)
X = df[["feature_name"]]
y = df["target"]

bagged_trees = BaggingRegressor()
bagged_trees.fit(X, y)
bagged_trees_predictions = bagged_trees.predict(X) # rises no warning
bagged_trees.estimators_[0].predict(X) # rises UserWarning

Expected Results

No warning should be thrown

Actual Results

/home/arturoamor/miniforge3/envs/scikit-learn-course/lib/python3.9/site-packages/sklearn/base.py:438: UserWarning: X has feature names, but DecisionTreeRegressor was fitted without feature names
warnings.warn(
array([72., 55., 32., 32.])

Versions

System:
python: 3.9.5 | packaged by conda-forge | (default, Jun 19 2021, 00:32:32) [GCC 9.3.0]
executable: /home/arturoamor/miniforge3/envs/scikit-learn-course/bin/python
machine: Linux-5.13.0-1017-oem-x86_64-with-glibc2.31

Python dependencies:
pip: 21.1.3
setuptools: 49.6.0.post20210108
sklearn: 1.0.1
numpy: 1.21.0
scipy: 1.7.0
Cython: None
pandas: 1.3.0
matplotlib: 3.4.2
joblib: 1.0.1
threadpoolctl: 2.1.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions