-
-
Notifications
You must be signed in to change notification settings - Fork 26.2k
ENH Specify categorical features with feature names in HGBDT #24889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2b2d894
fc0d51a
28e3397
1f5b46f
3bdcbb6
f9c3401
a4f9a3f
ec7337a
01d390d
c21a310
6241c47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,16 +193,43 @@ def _check_categories(self, X): | |
if categorical_features.size == 0: | ||
return None, None | ||
|
||
if categorical_features.dtype.kind not in ("i", "b"): | ||
if categorical_features.dtype.kind not in ("i", "b", "U", "O"): | ||
raise ValueError( | ||
"categorical_features must be an array-like of " | ||
"bools or array-like of ints." | ||
"categorical_features must be an array-like of bool, int or " | ||
f"str, got: {categorical_features.dtype.name}." | ||
) | ||
|
||
if categorical_features.dtype.kind == "O": | ||
types = set(type(f) for f in categorical_features) | ||
if types != {str}: | ||
raise ValueError( | ||
"categorical_features must be an array-like of bool, int or " | ||
f"str, got: {', '.join(sorted(t.__name__ for t in types))}." | ||
) | ||
|
||
n_features = X.shape[1] | ||
|
||
# check for categorical features as indices | ||
if categorical_features.dtype.kind == "i": | ||
if categorical_features.dtype.kind in ("U", "O"): | ||
# check for feature names | ||
if not hasattr(self, "feature_names_in_"): | ||
raise ValueError( | ||
"categorical_features should be passed as an array of " | ||
"integers or as a boolean mask when the model is fitted " | ||
"on data without feature names." | ||
) | ||
is_categorical = np.zeros(n_features, dtype=bool) | ||
feature_names = self.feature_names_in_.tolist() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this conversion to a list necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Arrays do not have the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the feature names list should never be to long (few hundred values) for HGBDT models in practice because those models tend to perform poorly when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation. |
||
for feature_name in categorical_features: | ||
try: | ||
is_categorical[feature_names.index(feature_name)] = True | ||
glemaitre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except ValueError as e: | ||
raise ValueError( | ||
f"categorical_features has a item value '{feature_name}' " | ||
"which is not a valid feature name of the training " | ||
f"data. Observed feature names: {feature_names}" | ||
) from e | ||
elif categorical_features.dtype.kind == "i": | ||
# check for categorical features as indices | ||
if ( | ||
np.max(categorical_features) >= n_features | ||
or np.min(categorical_features) < 0 | ||
|
@@ -1209,14 +1236,16 @@ class HistGradientBoostingRegressor(RegressorMixin, BaseHistGradientBoosting): | |
Features with a small number of unique values may use less than | ||
``max_bins`` bins. In addition to the ``max_bins`` bins, one more bin | ||
is always reserved for missing values. Must be no larger than 255. | ||
categorical_features : array-like of {bool, int} of shape (n_features) \ | ||
categorical_features : array-like of {bool, int, str} of shape (n_features) \ | ||
or shape (n_categorical_features,), default=None | ||
Indicates the categorical features. | ||
|
||
- None : no feature will be considered categorical. | ||
- boolean array-like : boolean mask indicating categorical features. | ||
- integer array-like : integer indices indicating categorical | ||
features. | ||
- str array-like: names of categorical features (assuming the training | ||
data has feature names). | ||
|
||
For each categorical feature, there must be at most `max_bins` unique | ||
categories, and each categorical value must be in [0, max_bins -1]. | ||
|
@@ -1227,6 +1256,9 @@ class HistGradientBoostingRegressor(RegressorMixin, BaseHistGradientBoosting): | |
|
||
.. versionadded:: 0.24 | ||
|
||
.. versionchanged:: 1.2 | ||
Added support for feature names. | ||
|
||
monotonic_cst : array-like of int of shape (n_features), default=None | ||
Indicates the monotonic constraint to enforce on each feature. | ||
- 1: monotonic increase | ||
|
@@ -1541,14 +1573,16 @@ class HistGradientBoostingClassifier(ClassifierMixin, BaseHistGradientBoosting): | |
Features with a small number of unique values may use less than | ||
``max_bins`` bins. In addition to the ``max_bins`` bins, one more bin | ||
is always reserved for missing values. Must be no larger than 255. | ||
categorical_features : array-like of {bool, int} of shape (n_features) \ | ||
categorical_features : array-like of {bool, int, str} of shape (n_features) \ | ||
or shape (n_categorical_features,), default=None | ||
Indicates the categorical features. | ||
|
||
- None : no feature will be considered categorical. | ||
- boolean array-like : boolean mask indicating categorical features. | ||
- integer array-like : integer indices indicating categorical | ||
features. | ||
- str array-like: names of categorical features (assuming the training | ||
data has feature names). | ||
|
||
For each categorical feature, there must be at most `max_bins` unique | ||
categories, and each categorical value must be in [0, max_bins -1]. | ||
|
@@ -1559,6 +1593,9 @@ class HistGradientBoostingClassifier(ClassifierMixin, BaseHistGradientBoosting): | |
|
||
.. versionadded:: 0.24 | ||
|
||
.. versionchanged:: 1.2 | ||
Added support for feature names. | ||
|
||
monotonic_cst : array-like of int of shape (n_features), default=None | ||
Indicates the monotonic constraint to enforce on each feature. | ||
- 1: monotonic increase | ||
|
Uh oh!
There was an error while loading. Please reload this page.