Refit the existing Booster by new data. Parameters ---------- data : string, numpy array, pandas DataFrame, H2O DataTable's Frame or scipy.sparse Data source for refit. If string, it represents the path to txt file. label : list, numpy 1-D arr
(self, data, label, decay_rate=0.9, **kwargs)
| 2484 | data_has_header, is_reshape) |
| 2485 | |
| 2486 | def refit(self, data, label, decay_rate=0.9, **kwargs): |
| 2487 | """Refit the existing Booster by new data. |
| 2488 | |
| 2489 | Parameters |
| 2490 | ---------- |
| 2491 | data : string, numpy array, pandas DataFrame, H2O DataTable's Frame or scipy.sparse |
| 2492 | Data source for refit. |
| 2493 | If string, it represents the path to txt file. |
| 2494 | label : list, numpy 1-D array or pandas Series / one-column DataFrame |
| 2495 | Label for refit. |
| 2496 | decay_rate : float, optional (default=0.9) |
| 2497 | Decay rate of refit, |
| 2498 | will use ``leaf_output = decay_rate * old_leaf_output + (1.0 - decay_rate) * new_leaf_output`` to refit trees. |
| 2499 | **kwargs |
| 2500 | Other parameters for refit. |
| 2501 | These parameters will be passed to ``predict`` method. |
| 2502 | |
| 2503 | Returns |
| 2504 | ------- |
| 2505 | result : Booster |
| 2506 | Refitted Booster. |
| 2507 | """ |
| 2508 | if self.__set_objective_to_none: |
| 2509 | raise LightGBMError('Cannot refit due to null objective function.') |
| 2510 | predictor = self._to_predictor(copy.deepcopy(kwargs)) |
| 2511 | leaf_preds = predictor.predict(data, -1, pred_leaf=True) |
| 2512 | nrow, ncol = leaf_preds.shape |
| 2513 | train_set = Dataset(data, label, silent=True) |
| 2514 | new_params = copy.deepcopy(self.params) |
| 2515 | new_params['refit_decay_rate'] = decay_rate |
| 2516 | new_booster = Booster(new_params, train_set, silent=True) |
| 2517 | # Copy models |
| 2518 | _safe_call(_LIB.LGBM_BoosterMerge( |
| 2519 | new_booster.handle, |
| 2520 | predictor.handle)) |
| 2521 | leaf_preds = leaf_preds.reshape(-1) |
| 2522 | ptr_data, type_ptr_data, _ = c_int_array(leaf_preds) |
| 2523 | _safe_call(_LIB.LGBM_BoosterRefit( |
| 2524 | new_booster.handle, |
| 2525 | ptr_data, |
| 2526 | ctypes.c_int(nrow), |
| 2527 | ctypes.c_int(ncol))) |
| 2528 | new_booster.network = self.network |
| 2529 | new_booster.__attr = self.__attr.copy() |
| 2530 | return new_booster |
| 2531 | |
| 2532 | def get_leaf_output(self, tree_id, leaf_id): |
| 2533 | """Get the output of a leaf. |