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

Skip to content

Commit 575da32

Browse files
committed
Make comments better
1 parent a838f2e commit 575da32

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

sklearn/ensemble/_bagging.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ def _parallel_build_estimators(
159159
sample_weight,
160160
)
161161

162-
# Pass the resolved categorical mask from the ensemble. Trees only see the
163-
# already-encoded float ndarray, so user values like "from_dtype" or
164-
# feature-name lists would otherwise silently resolve to None.
162+
# Trees must get the bool mask, not categorical_features="from_dtype" (or
163+
# column names). The ensemble already turned X into a NumPy array, so trees
164+
# can no longer read dtypes/names and would treat all features as numeric.
165165
if getattr(ensemble, "is_categorical_", None) is not None:
166166
if requires_feature_indexing:
167167
cat_subset = ensemble.is_categorical_[features]

sklearn/ensemble/_forest.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ def fit(self, X, y, sample_weight=None):
354354
skip_check_array=True,
355355
)
356356
X = self._preprocess_X(X, reset=True)
357-
# X has already been encoded to a numeric array. Do not call
358-
# validate_data(reset=True) again here because ndarray input would
359-
# remove feature_names_in_ captured from the original container.
357+
# Feature names were already stored from the original dataframe above.
358+
# Encoding turns X into a plain ndarray with no names.
359+
# validate_data(reset=True) would treat that as "no feature names"
360+
# and delete feature_names_in_.
360361
X, y = validate_data(
361362
self,
362363
X,
@@ -497,9 +498,9 @@ def fit(self, X, y, sample_weight=None):
497498
self._make_estimator(append=False, random_state=random_state)
498499
for i in range(n_more_estimators)
499500
]
500-
# Pass the resolved categorical mask, not the user parameter. After the
501-
# forest encodes X, trees only see a float ndarray so values like
502-
# "from_dtype" or feature-name lists would silently resolve to None.
501+
# Trees must get the bool mask, not categorical_features="from_dtype" (or
502+
# column names). The forest already turned X into a NumPy array, so trees
503+
# can no longer read dtypes/names and would treat all features as numeric.
503504
if self.is_categorical_ is not None:
504505
for tree in trees:
505506
tree.set_params(categorical_features=self.is_categorical_)

sklearn/tree/_classes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,10 @@ def _fit(
303303
)
304304
check_y_params = dict(ensure_2d=False, dtype=None)
305305
if has_categorical:
306-
# X has already been encoded to a numeric array. Do not call
307-
# validate_data(reset=True) again here because ndarray input would
308-
# remove feature_names_in_ captured from the original container.
306+
# Feature names were already stored from the original dataframe above.
307+
# Encoding turns X into a plain ndarray with no names.
308+
# validate_data(reset=True) would treat that as "no feature names" and
309+
# delete feature_names_in_.
309310
X = check_array(X, input_name="X", estimator=self, **check_X_params)
310311
y = check_array(y, input_name="y", estimator=self, **check_y_params)
311312
_check_n_features(self, X, reset=False)

0 commit comments

Comments
 (0)