Proxy class for evaluation function.
| 95 | |
| 96 | |
| 97 | class _EvalFunctionWrapper(object): |
| 98 | """Proxy class for evaluation function.""" |
| 99 | |
| 100 | def __init__(self, func): |
| 101 | """Construct a proxy class. |
| 102 | |
| 103 | This class transforms evaluation function to match evaluation function with signature ``new_func(preds, dataset)`` |
| 104 | as expected by ``lightgbm.engine.train``. |
| 105 | |
| 106 | Parameters |
| 107 | ---------- |
| 108 | func : callable |
| 109 | Expects a callable with following signatures: |
| 110 | ``func(y_true, y_pred)``, |
| 111 | ``func(y_true, y_pred, weight)`` |
| 112 | or ``func(y_true, y_pred, weight, group)`` |
| 113 | and returns (eval_name, eval_result, is_higher_better) or |
| 114 | list of (eval_name, eval_result, is_higher_better): |
| 115 | |
| 116 | y_true : array-like of shape = [n_samples] |
| 117 | The target values. |
| 118 | y_pred : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task) |
| 119 | The predicted values. |
| 120 | weight : array-like of shape = [n_samples] |
| 121 | The weight of samples. |
| 122 | group : array-like |
| 123 | Group/query data, used for ranking task. |
| 124 | eval_name : string |
| 125 | The name of evaluation function (without whitespaces). |
| 126 | eval_result : float |
| 127 | The eval result. |
| 128 | is_higher_better : bool |
| 129 | Is eval result higher better, e.g. AUC is ``is_higher_better``. |
| 130 | |
| 131 | .. note:: |
| 132 | |
| 133 | For multi-class task, the y_pred is group by class_id first, then group by row_id. |
| 134 | If you want to get i-th row y_pred in j-th class, the access way is y_pred[j * num_data + i]. |
| 135 | """ |
| 136 | self.func = func |
| 137 | |
| 138 | def __call__(self, preds, dataset): |
| 139 | """Call passed function with appropriate arguments. |
| 140 | |
| 141 | Parameters |
| 142 | ---------- |
| 143 | preds : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task) |
| 144 | The predicted values. |
| 145 | dataset : Dataset |
| 146 | The training dataset. |
| 147 | |
| 148 | Returns |
| 149 | ------- |
| 150 | eval_name : string |
| 151 | The name of evaluation function (without whitespaces). |
| 152 | eval_result : float |
| 153 | The eval result. |
| 154 | is_higher_better : bool |