Define feature_names_in_. (#7526)

* Define `feature_names_in_`.
* Raise attribute error if it's not defined.
This commit is contained in:
Jiaming Yuan
2022-01-05 01:35:34 +08:00
committed by GitHub
parent 8f0a42a266
commit eb1efb54b5
2 changed files with 17 additions and 0 deletions

View File

@@ -1105,6 +1105,18 @@ class XGBModel(XGBModelBase):
booster = self.get_booster()
return booster.num_features()
@property
def feature_names_in_(self) -> np.ndarray:
"""Names of features seen during :py:meth:`fit`. Defined only when `X` has feature
names that are all strings."""
feature_names = self.get_booster().feature_names
if feature_names is None:
raise AttributeError(
"`feature_names_in_` is defined only when `X` has feature names that "
"are all strings."
)
return np.array(feature_names)
def _early_stopping_attr(self, attr: str) -> Union[float, int]:
booster = self.get_booster()
try: