Unverified Commit c3cf335c authored by James Lamb's avatar James Lamb Committed by GitHub
Browse files

[python-package] add more hints in sklearn.py (#5460)

parent 8b105ceb
...@@ -22,8 +22,8 @@ from .compat import (DASK_INSTALLED, PANDAS_INSTALLED, SKLEARN_INSTALLED, Client ...@@ -22,8 +22,8 @@ from .compat import (DASK_INSTALLED, PANDAS_INSTALLED, SKLEARN_INSTALLED, Client
dask_Array, dask_array_from_delayed, dask_bag_from_delayed, dask_DataFrame, dask_Series, dask_Array, dask_array_from_delayed, dask_bag_from_delayed, dask_DataFrame, dask_Series,
default_client, delayed, pd_DataFrame, pd_Series, wait) default_client, delayed, pd_DataFrame, pd_Series, wait)
from .sklearn import (LGBMClassifier, LGBMModel, LGBMRanker, LGBMRegressor, _LGBM_ScikitCustomEvalFunction, from .sklearn import (LGBMClassifier, LGBMModel, LGBMRanker, LGBMRegressor, _LGBM_ScikitCustomEvalFunction,
_LGBM_ScikitCustomObjectiveFunction, _lgbmmodel_doc_custom_eval_note, _lgbmmodel_doc_fit, _LGBM_ScikitCustomObjectiveFunction, _LGBM_ScikitEvalMetricType, _lgbmmodel_doc_custom_eval_note,
_lgbmmodel_doc_predict) _lgbmmodel_doc_fit, _lgbmmodel_doc_predict)
_DaskCollection = Union[dask_Array, dask_DataFrame, dask_Series] _DaskCollection = Union[dask_Array, dask_DataFrame, dask_Series]
_DaskMatrixLike = Union[dask_Array, dask_DataFrame] _DaskMatrixLike = Union[dask_Array, dask_DataFrame]
...@@ -405,8 +405,8 @@ def _train( ...@@ -405,8 +405,8 @@ def _train(
eval_class_weight: Optional[List[Union[dict, str]]] = None, eval_class_weight: Optional[List[Union[dict, str]]] = None,
eval_init_score: Optional[List[_DaskCollection]] = None, eval_init_score: Optional[List[_DaskCollection]] = None,
eval_group: Optional[List[_DaskVectorLike]] = None, eval_group: Optional[List[_DaskVectorLike]] = None,
eval_metric: Optional[Union[_LGBM_ScikitCustomEvalFunction, str, List[Union[_LGBM_ScikitCustomEvalFunction, str]]]] = None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
eval_at: Optional[Iterable[int]] = None, eval_at: Optional[Union[List[int], Tuple[int]]] = None,
**kwargs: Any **kwargs: Any
) -> LGBMModel: ) -> LGBMModel:
"""Inner train routine. """Inner train routine.
...@@ -454,7 +454,7 @@ def _train( ...@@ -454,7 +454,7 @@ def _train(
If list, it can be a list of built-in metrics, a list of custom evaluation metrics, or a mix of both. If list, it can be a list of built-in metrics, a list of custom evaluation metrics, or a mix of both.
In either case, the ``metric`` from the Dask model parameters (or inferred from the objective) will be evaluated and used as well. In either case, the ``metric`` from the Dask model parameters (or inferred from the objective) will be evaluated and used as well.
Default: 'l2' for DaskLGBMRegressor, 'binary(multi)_logloss' for DaskLGBMClassifier, 'ndcg' for DaskLGBMRanker. Default: 'l2' for DaskLGBMRegressor, 'binary(multi)_logloss' for DaskLGBMClassifier, 'ndcg' for DaskLGBMRanker.
eval_at : iterable of int, optional (default=None) eval_at : list or tuple of int, optional (default=None)
The evaluation positions of the specified ranking metric. The evaluation positions of the specified ranking metric.
**kwargs **kwargs
Other parameters passed to ``fit`` method of the local underlying model. Other parameters passed to ``fit`` method of the local underlying model.
...@@ -1037,7 +1037,7 @@ class _DaskLGBMModel: ...@@ -1037,7 +1037,7 @@ class _DaskLGBMModel:
eval_class_weight: Optional[List[Union[dict, str]]] = None, eval_class_weight: Optional[List[Union[dict, str]]] = None,
eval_init_score: Optional[List[_DaskCollection]] = None, eval_init_score: Optional[List[_DaskCollection]] = None,
eval_group: Optional[List[_DaskVectorLike]] = None, eval_group: Optional[List[_DaskVectorLike]] = None,
eval_metric: Optional[Union[_LGBM_ScikitCustomEvalFunction, str, List[Union[_LGBM_ScikitCustomEvalFunction, str]]]] = None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
eval_at: Optional[Iterable[int]] = None, eval_at: Optional[Iterable[int]] = None,
**kwargs: Any **kwargs: Any
) -> "_DaskLGBMModel": ) -> "_DaskLGBMModel":
...@@ -1163,7 +1163,7 @@ class DaskLGBMClassifier(LGBMClassifier, _DaskLGBMModel): ...@@ -1163,7 +1163,7 @@ class DaskLGBMClassifier(LGBMClassifier, _DaskLGBMModel):
eval_sample_weight: Optional[List[_DaskVectorLike]] = None, eval_sample_weight: Optional[List[_DaskVectorLike]] = None,
eval_class_weight: Optional[List[Union[dict, str]]] = None, eval_class_weight: Optional[List[Union[dict, str]]] = None,
eval_init_score: Optional[List[_DaskCollection]] = None, eval_init_score: Optional[List[_DaskCollection]] = None,
eval_metric: Optional[Union[_LGBM_ScikitCustomEvalFunction, str, List[Union[_LGBM_ScikitCustomEvalFunction, str]]]] = None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
**kwargs: Any **kwargs: Any
) -> "DaskLGBMClassifier": ) -> "DaskLGBMClassifier":
"""Docstring is inherited from the lightgbm.LGBMClassifier.fit.""" """Docstring is inherited from the lightgbm.LGBMClassifier.fit."""
...@@ -1334,7 +1334,7 @@ class DaskLGBMRegressor(LGBMRegressor, _DaskLGBMModel): ...@@ -1334,7 +1334,7 @@ class DaskLGBMRegressor(LGBMRegressor, _DaskLGBMModel):
eval_names: Optional[List[str]] = None, eval_names: Optional[List[str]] = None,
eval_sample_weight: Optional[List[_DaskVectorLike]] = None, eval_sample_weight: Optional[List[_DaskVectorLike]] = None,
eval_init_score: Optional[List[_DaskVectorLike]] = None, eval_init_score: Optional[List[_DaskVectorLike]] = None,
eval_metric: Optional[Union[_LGBM_ScikitCustomEvalFunction, str, List[Union[_LGBM_ScikitCustomEvalFunction, str]]]] = None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
**kwargs: Any **kwargs: Any
) -> "DaskLGBMRegressor": ) -> "DaskLGBMRegressor":
"""Docstring is inherited from the lightgbm.LGBMRegressor.fit.""" """Docstring is inherited from the lightgbm.LGBMRegressor.fit."""
...@@ -1489,8 +1489,8 @@ class DaskLGBMRanker(LGBMRanker, _DaskLGBMModel): ...@@ -1489,8 +1489,8 @@ class DaskLGBMRanker(LGBMRanker, _DaskLGBMModel):
eval_sample_weight: Optional[List[_DaskVectorLike]] = None, eval_sample_weight: Optional[List[_DaskVectorLike]] = None,
eval_init_score: Optional[List[_DaskVectorLike]] = None, eval_init_score: Optional[List[_DaskVectorLike]] = None,
eval_group: Optional[List[_DaskVectorLike]] = None, eval_group: Optional[List[_DaskVectorLike]] = None,
eval_metric: Optional[Union[_LGBM_ScikitCustomEvalFunction, str, List[Union[_LGBM_ScikitCustomEvalFunction, str]]]] = None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
eval_at: Iterable[int] = (1, 2, 3, 4, 5), eval_at: Union[List[int], Tuple[int]] = (1, 2, 3, 4, 5),
**kwargs: Any **kwargs: Any
) -> "DaskLGBMRanker": ) -> "DaskLGBMRanker":
"""Docstring is inherited from the lightgbm.LGBMRanker.fit.""" """Docstring is inherited from the lightgbm.LGBMRanker.fit."""
...@@ -1527,7 +1527,7 @@ class DaskLGBMRanker(LGBMRanker, _DaskLGBMModel): ...@@ -1527,7 +1527,7 @@ class DaskLGBMRanker(LGBMRanker, _DaskLGBMModel):
+ _base_doc[_base_doc.find('eval_init_score :'):]) + _base_doc[_base_doc.find('eval_init_score :'):])
_base_doc = (_base_doc[:_base_doc.find('feature_name :')] _base_doc = (_base_doc[:_base_doc.find('feature_name :')]
+ "eval_at : iterable of int, optional (default=(1, 2, 3, 4, 5))\n" + "eval_at : list or tuple of int, optional (default=(1, 2, 3, 4, 5))\n"
+ f"{' ':8}The evaluation positions of the specified metric.\n" + f"{' ':8}The evaluation positions of the specified metric.\n"
+ f"{' ':4}{_base_doc[_base_doc.find('feature_name :'):]}") + f"{' ':4}{_base_doc[_base_doc.find('feature_name :'):]}")
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
"""Scikit-learn wrapper interface for LightGBM.""" """Scikit-learn wrapper interface for LightGBM."""
import copy import copy
from inspect import signature from inspect import signature
from typing import Any, Callable, Dict, List, Optional, Tuple, Union from pathlib import Path
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
import numpy as np import numpy as np
...@@ -43,6 +44,11 @@ _LGBM_ScikitCustomEvalFunction = Union[ ...@@ -43,6 +44,11 @@ _LGBM_ScikitCustomEvalFunction = Union[
Union[_LGBM_EvalFunctionResultType, List[_LGBM_EvalFunctionResultType]] Union[_LGBM_EvalFunctionResultType, List[_LGBM_EvalFunctionResultType]]
], ],
] ]
_LGBM_ScikitEvalMetricType = Union[
str,
_LGBM_ScikitCustomEvalFunction,
List[Union[str, _LGBM_ScikitCustomEvalFunction]]
]
class _ObjectiveFunctionWrapper: class _ObjectiveFunctionWrapper:
...@@ -686,16 +692,16 @@ class LGBMModel(_LGBMModelBase): ...@@ -686,16 +692,16 @@ class LGBMModel(_LGBMModelBase):
init_score=None, init_score=None,
group=None, group=None,
eval_set=None, eval_set=None,
eval_names=None, eval_names: Optional[List[str]] = None,
eval_sample_weight=None, eval_sample_weight=None,
eval_class_weight=None, eval_class_weight=None,
eval_init_score=None, eval_init_score=None,
eval_group=None, eval_group=None,
eval_metric=None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
feature_name='auto', feature_name='auto',
categorical_feature='auto', categorical_feature='auto',
callbacks=None, callbacks=None,
init_model=None init_model: Optional[Union[str, Path, Booster, "LGBMModel"]] = None
): ):
"""Docstring is set after definition, using a template.""" """Docstring is set after definition, using a template."""
params = self._process_params(stage="fit") params = self._process_params(stage="fit")
...@@ -979,14 +985,14 @@ class LGBMRegressor(_LGBMRegressorBase, LGBMModel): ...@@ -979,14 +985,14 @@ class LGBMRegressor(_LGBMRegressorBase, LGBMModel):
sample_weight=None, sample_weight=None,
init_score=None, init_score=None,
eval_set=None, eval_set=None,
eval_names=None, eval_names: Optional[List[str]] = None,
eval_sample_weight=None, eval_sample_weight=None,
eval_init_score=None, eval_init_score=None,
eval_metric=None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
feature_name='auto', feature_name='auto',
categorical_feature='auto', categorical_feature='auto',
callbacks=None, callbacks=None,
init_model=None init_model: Optional[Union[str, Path, Booster, LGBMModel]] = None
): ):
"""Docstring is inherited from the LGBMModel.""" """Docstring is inherited from the LGBMModel."""
super().fit( super().fit(
...@@ -1025,15 +1031,15 @@ class LGBMClassifier(_LGBMClassifierBase, LGBMModel): ...@@ -1025,15 +1031,15 @@ class LGBMClassifier(_LGBMClassifierBase, LGBMModel):
sample_weight=None, sample_weight=None,
init_score=None, init_score=None,
eval_set=None, eval_set=None,
eval_names=None, eval_names: Optional[List[str]] = None,
eval_sample_weight=None, eval_sample_weight=None,
eval_class_weight=None, eval_class_weight=None,
eval_init_score=None, eval_init_score=None,
eval_metric=None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
feature_name='auto', feature_name='auto',
categorical_feature='auto', categorical_feature='auto',
callbacks=None, callbacks=None,
init_model=None init_model: Optional[Union[str, Path, Booster, LGBMModel]] = None
): ):
"""Docstring is inherited from the LGBMModel.""" """Docstring is inherited from the LGBMModel."""
_LGBMAssertAllFinite(y) _LGBMAssertAllFinite(y)
...@@ -1187,16 +1193,16 @@ class LGBMRanker(LGBMModel): ...@@ -1187,16 +1193,16 @@ class LGBMRanker(LGBMModel):
init_score=None, init_score=None,
group=None, group=None,
eval_set=None, eval_set=None,
eval_names=None, eval_names: Optional[List[str]] = None,
eval_sample_weight=None, eval_sample_weight=None,
eval_init_score=None, eval_init_score=None,
eval_group=None, eval_group=None,
eval_metric=None, eval_metric: Optional[_LGBM_ScikitEvalMetricType] = None,
eval_at=(1, 2, 3, 4, 5), eval_at: Union[List[int], Tuple[int]] = (1, 2, 3, 4, 5),
feature_name='auto', feature_name='auto',
categorical_feature='auto', categorical_feature='auto',
callbacks=None, callbacks=None,
init_model=None init_model: Optional[Union[str, Path, Booster, LGBMModel]] = None
): ):
"""Docstring is inherited from the LGBMModel.""" """Docstring is inherited from the LGBMModel."""
# check group data # check group data
...@@ -1240,6 +1246,6 @@ class LGBMRanker(LGBMModel): ...@@ -1240,6 +1246,6 @@ class LGBMRanker(LGBMModel):
+ _base_doc[_base_doc.find('eval_init_score :'):]) # type: ignore + _base_doc[_base_doc.find('eval_init_score :'):]) # type: ignore
_base_doc = fit.__doc__ _base_doc = fit.__doc__
_before_feature_name, _feature_name, _after_feature_name = _base_doc.partition('feature_name :') _before_feature_name, _feature_name, _after_feature_name = _base_doc.partition('feature_name :')
fit.__doc__ = f"""{_before_feature_name}eval_at : iterable of int, optional (default=(1, 2, 3, 4, 5)) fit.__doc__ = f"""{_before_feature_name}eval_at : list or tuple of int, optional (default=(1, 2, 3, 4, 5))
The evaluation positions of the specified metric. The evaluation positions of the specified metric.
{_feature_name}{_after_feature_name}""" {_feature_name}{_after_feature_name}"""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment