-
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] add type hints on train() in engine.py #4544
Conversation
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.
Oh, that was a hard work, thanks!
sklearn-wrapper and hence Dask-package use callables with another signature for custom objective and metric functions:
LightGBM/python-package/lightgbm/sklearn.py
Lines 23 to 49 in 4e18c60
This class transforms objective function to match objective function with signature ``new_func(preds, dataset)`` | |
as expected by ``lightgbm.engine.train``. | |
Parameters | |
---------- | |
func : callable | |
Expects a callable with signature ``func(y_true, y_pred)`` or ``func(y_true, y_pred, group) | |
and returns (grad, hess): | |
y_true : array-like of shape = [n_samples] | |
The target values. | |
y_pred : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task) | |
The predicted values. | |
Predicted values are returned before any transformation, | |
e.g. they are raw margin instead of probability of positive class for binary task. | |
group : array-like | |
Group/query data. | |
Only used in the learning-to-rank task. | |
sum(group) = n_samples. | |
For example, if you have a 100-document dataset with ``group = [10, 20, 40, 10, 10, 10]``, that means that you have 6 groups, | |
where the first 10 records are in the first group, records 11-30 are in the second group, records 31-70 are in the third group, etc. | |
grad : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task) | |
The value of the first order derivative (gradient) of the loss | |
with respect to the elements of y_pred for each sample point. | |
hess : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task) | |
The value of the second order derivative (Hessian) of the loss | |
with respect to the elements of y_pred for each sample point. |
LightGBM/python-package/lightgbm/sklearn.py
Lines 112 to 144 in 4e18c60
This class transforms evaluation function to match evaluation function with signature ``new_func(preds, dataset)`` | |
as expected by ``lightgbm.engine.train``. | |
Parameters | |
---------- | |
func : callable | |
Expects a callable with following signatures: | |
``func(y_true, y_pred)``, | |
``func(y_true, y_pred, weight)`` | |
or ``func(y_true, y_pred, weight, group)`` | |
and returns (eval_name, eval_result, is_higher_better) or | |
list of (eval_name, eval_result, is_higher_better): | |
y_true : array-like of shape = [n_samples] | |
The target values. | |
y_pred : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task) | |
The predicted values. | |
In case of custom ``objective``, predicted values are returned before any transformation, | |
e.g. they are raw margin instead of probability of positive class for binary task in this case. | |
weight : array-like of shape = [n_samples] | |
The weight of samples. | |
group : array-like | |
Group/query data. | |
Only used in the learning-to-rank task. | |
sum(group) = n_samples. | |
For example, if you have a 100-document dataset with ``group = [10, 20, 40, 10, 10, 10]``, that means that you have 6 groups, | |
where the first 10 records are in the first group, records 11-30 are in the second group, records 31-70 are in the third group, etc. | |
eval_name : string | |
The name of evaluation function (without whitespace). | |
eval_result : float | |
The eval result. | |
is_higher_better : bool | |
Is eval result higher better, e.g. AUC is ``is_higher_better``. |
oh interesting, all this time I didn't know that the interfaces were different! I'll remove those changes from this PR and make them in a separate one. |
Nice plan, thanks! |
Co-authored-by: Nikita Titov <[email protected]>
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.
Great job, many thanks!
This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this. |
Proposes adding type hints on
train()
, as part of #3756.