-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
[python-package] add type hints on cv() #5271
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,19 +4,24 @@ | |
import copy | ||
from operator import attrgetter | ||
from pathlib import Path | ||
from typing import Any, Callable, Dict, List, Optional, Tuple, Union | ||
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union | ||
|
||
import numpy as np | ||
|
||
from . import callback | ||
from .basic import Booster, Dataset, LightGBMError, _choose_param_value, _ConfigAliases, _InnerPredictor, _log_warning | ||
from .compat import SKLEARN_INSTALLED, _LGBMGroupKFold, _LGBMStratifiedKFold | ||
from .compat import SKLEARN_INSTALLED, _LGBMBaseCrossValidator, _LGBMGroupKFold, _LGBMStratifiedKFold | ||
|
||
_LGBM_CustomMetricFunction = Callable[ | ||
[np.ndarray, Dataset], | ||
Tuple[str, float, bool] | ||
] | ||
|
||
_LGBM_PreprocFunction = Callable[ | ||
[Dataset, Dataset, Dict[str, Any]], | ||
Tuple[Dataset, Dataset, Dict[str, Any]] | ||
] | ||
|
||
|
||
def train( | ||
params: Dict[str, Any], | ||
|
@@ -373,12 +378,25 @@ def _agg_cv_result(raw_results): | |
return [('cv_agg', k, np.mean(v), metric_type[k], np.std(v)) for k, v in cvmap.items()] | ||
|
||
|
||
def cv(params, train_set, num_boost_round=100, | ||
folds=None, nfold=5, stratified=True, shuffle=True, | ||
metrics=None, feval=None, init_model=None, | ||
feature_name='auto', categorical_feature='auto', | ||
fpreproc=None, seed=0, callbacks=None, eval_train_metric=False, | ||
return_cvbooster=False): | ||
def cv( | ||
params: Dict[str, Any], | ||
train_set: Dataset, | ||
num_boost_round: int = 100, | ||
folds: Optional[Union[Iterable[Tuple[np.ndarray, np.ndarray]], _LGBMBaseCrossValidator]] = None, | ||
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. according to https://docs.python.org/3/library/typing.html#typing.Generator, it's ok to use |
||
nfold: int = 5, | ||
stratified: bool = True, | ||
shuffle: bool = True, | ||
metrics: Optional[Union[str, List[str]]] = None, | ||
feval: Optional[Union[_LGBM_CustomMetricFunction, List[_LGBM_CustomMetricFunction]]] = None, | ||
init_model: Optional[Union[str, Path, Booster]] = None, | ||
feature_name: Union[str, List[str]] = 'auto', | ||
categorical_feature: Union[str, List[str], List[int]] = 'auto', | ||
fpreproc: Optional[_LGBM_PreprocFunction] = None, | ||
seed: int = 0, | ||
callbacks: Optional[List[Callable]] = None, | ||
eval_train_metric: bool = False, | ||
return_cvbooster: bool = False | ||
) -> Dict[str, Any]: | ||
"""Perform the cross-validation with given parameters. | ||
|
||
Parameters | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sklearn.model_selection.BaseCrossValidator
has been inscikit-learn
for at least 7 years, so I don't think this will cause any portability issues.https://github.com/scikit-learn/scikit-learn/blame/159cb46c5672b0ba87d0ba80c6b80ec1aa5fda32/sklearn/model_selection/__init__.py#L3