Medial Code Documentation
Loading...
Searching...
No Matches
Public Member Functions | Data Fields | Protected Member Functions
lightgbm.basic.Booster Class Reference
Inheritance diagram for lightgbm.basic.Booster:

Public Member Functions

 __init__ (self, params=None, train_set=None, model_file=None, silent=False)
 
 __del__ (self)
 
 __copy__ (self)
 
 __deepcopy__ (self, _)
 
 __getstate__ (self)
 
 __setstate__ (self, state)
 
 free_dataset (self)
 
 set_network (self, machines, local_listen_port=12400, listen_time_out=120, num_machines=1)
 
 free_network (self)
 
 set_train_data_name (self, name)
 
 add_valid (self, data, name)
 
 reset_parameter (self, params)
 
 update (self, train_set=None, fobj=None)
 
 rollback_one_iter (self)
 
 current_iteration (self)
 
 num_model_per_iteration (self)
 
 num_trees (self)
 
 eval (self, data, name, feval=None)
 
 eval_train (self, feval=None)
 
 eval_valid (self, feval=None)
 
 save_model (self, filename, num_iteration=None, start_iteration=0)
 
 shuffle_models (self, start_iteration=0, end_iteration=-1)
 
 model_from_string (self, model_str, verbose=True)
 
 model_to_string (self, num_iteration=None, start_iteration=0)
 
 dump_model (self, num_iteration=None, start_iteration=0)
 
 predict (self, data, num_iteration=None, raw_score=False, pred_leaf=False, pred_contrib=False, data_has_header=False, is_reshape=True, **kwargs)
 
 refit (self, data, label, decay_rate=0.9, **kwargs)
 
 get_leaf_output (self, tree_id, leaf_id)
 
 num_feature (self)
 
 feature_name (self)
 
 feature_importance (self, importance_type='split', iteration=None)
 
 attr (self, key)
 
 set_attr (self, **kwargs)
 

Data Fields

 handle
 
 network
 
 best_iteration
 
 best_score
 
 train_set
 
 valid_sets
 
 name_valid_sets
 
 pandas_categorical
 
 params
 

Protected Member Functions

 _free_buffer (self)
 
 _to_predictor (self, pred_parameter=None)
 

Detailed Description

Booster in LightGBM.

Constructor & Destructor Documentation

◆ __init__()

lightgbm.basic.Booster.__init__ (   self,
  params = None,
  train_set = None,
  model_file = None,
  silent = False 
)
Initialize the Booster.

Parameters
----------
params : dict or None, optional (default=None)
    Parameters for Booster.
train_set : Dataset or None, optional (default=None)
    Training dataset.
model_file : string or None, optional (default=None)
    Path to the model file.
silent : bool, optional (default=False)
    Whether to print messages during construction.

Member Function Documentation

◆ _to_predictor()

lightgbm.basic.Booster._to_predictor (   self,
  pred_parameter = None 
)
protected
Convert to predictor.

◆ add_valid()

lightgbm.basic.Booster.add_valid (   self,
  data,
  name 
)
Add validation data.

Parameters
----------
data : Dataset
    Validation data.
name : string
    Name of validation data.

Returns
-------
self : Booster
    Booster with set validation data.

◆ attr()

lightgbm.basic.Booster.attr (   self,
  key 
)
Get attribute string from the Booster.

Parameters
----------
key : string
    The name of the attribute.

Returns
-------
value : string or None
    The attribute value.
    Returns None if attribute does not exist.

◆ current_iteration()

lightgbm.basic.Booster.current_iteration (   self)
Get the index of the current iteration.

Returns
-------
cur_iter : int
    The index of the current iteration.

◆ dump_model()

lightgbm.basic.Booster.dump_model (   self,
  num_iteration = None,
  start_iteration = 0 
)
Dump Booster to JSON format.

Parameters
----------
num_iteration : int or None, optional (default=None)
    Index of the iteration that should be dumped.
    If None, if the best iteration exists, it is dumped; otherwise, all iterations are dumped.
    If <= 0, all iterations are dumped.
start_iteration : int, optional (default=0)
    Start index of the iteration that should be dumped.

Returns
-------
json_repr : dict
    JSON format of Booster.

◆ eval()

lightgbm.basic.Booster.eval (   self,
  data,
  name,
  feval = None 
)
Evaluate for data.

Parameters
----------
data : Dataset
    Data for the evaluating.
name : string
    Name of the data.
feval : callable or None, optional (default=None)
    Customized evaluation function.
    Should accept two parameters: preds, train_data,
    and return (eval_name, eval_result, is_higher_better) or list of such tuples.
    For multi-class task, the preds is group by class_id first, then group by row_id.
    If you want to get i-th row preds in j-th class, the access way is preds[j * num_data + i].

Returns
-------
result : list
    List with evaluation results.

◆ eval_train()

lightgbm.basic.Booster.eval_train (   self,
  feval = None 
)
Evaluate for training data.

Parameters
----------
feval : callable or None, optional (default=None)
    Customized evaluation function.
    Should accept two parameters: preds, train_data,
    and return (eval_name, eval_result, is_higher_better) or list of such tuples.
    For multi-class task, the preds is group by class_id first, then group by row_id.
    If you want to get i-th row preds in j-th class, the access way is preds[j * num_data + i].

Returns
-------
result : list
    List with evaluation results.

◆ eval_valid()

lightgbm.basic.Booster.eval_valid (   self,
  feval = None 
)
Evaluate for validation data.

Parameters
----------
feval : callable or None, optional (default=None)
    Customized evaluation function.
    Should accept two parameters: preds, train_data,
    and return (eval_name, eval_result, is_higher_better) or list of such tuples.
    For multi-class task, the preds is group by class_id first, then group by row_id.
    If you want to get i-th row preds in j-th class, the access way is preds[j * num_data + i].

Returns
-------
result : list
    List with evaluation results.

◆ feature_importance()

lightgbm.basic.Booster.feature_importance (   self,
  importance_type = 'split',
  iteration = None 
)
Get feature importances.

Parameters
----------
importance_type : string, optional (default="split")
    How the importance is calculated.
    If "split", result contains numbers of times the feature is used in a model.
    If "gain", result contains total gains of splits which use the feature.
iteration : int or None, optional (default=None)
    Limit number of iterations in the feature importance calculation.
    If None, if the best iteration exists, it is used; otherwise, all trees are used.
    If <= 0, all trees are used (no limits).

Returns
-------
result : numpy array
    Array with feature importances.

◆ feature_name()

lightgbm.basic.Booster.feature_name (   self)
Get names of features.

Returns
-------
result : list
    List with names of features.

◆ free_dataset()

lightgbm.basic.Booster.free_dataset (   self)
Free Booster's Datasets.

Returns
-------
self : Booster
    Booster without Datasets.

◆ free_network()

lightgbm.basic.Booster.free_network (   self)
Free Booster's network.

Returns
-------
self : Booster
    Booster with freed network.

◆ get_leaf_output()

lightgbm.basic.Booster.get_leaf_output (   self,
  tree_id,
  leaf_id 
)
Get the output of a leaf.

Parameters
----------
tree_id : int
    The index of the tree.
leaf_id : int
    The index of the leaf in the tree.

Returns
-------
result : float
    The output of the leaf.

◆ model_from_string()

lightgbm.basic.Booster.model_from_string (   self,
  model_str,
  verbose = True 
)
Load Booster from a string.

Parameters
----------
model_str : string
    Model will be loaded from this string.
verbose : bool, optional (default=True)
    Whether to print messages while loading model.

Returns
-------
self : Booster
    Loaded Booster object.

◆ model_to_string()

lightgbm.basic.Booster.model_to_string (   self,
  num_iteration = None,
  start_iteration = 0 
)
Save Booster to string.

Parameters
----------
num_iteration : int or None, optional (default=None)
    Index of the iteration that should be saved.
    If None, if the best iteration exists, it is saved; otherwise, all iterations are saved.
    If <= 0, all iterations are saved.
start_iteration : int, optional (default=0)
    Start index of the iteration that should be saved.

Returns
-------
str_repr : string
    String representation of Booster.

◆ num_feature()

lightgbm.basic.Booster.num_feature (   self)
Get number of features.

Returns
-------
num_feature : int
    The number of features.

◆ num_model_per_iteration()

lightgbm.basic.Booster.num_model_per_iteration (   self)
Get number of models per iteration.

Returns
-------
model_per_iter : int
    The number of models per iteration.

◆ num_trees()

lightgbm.basic.Booster.num_trees (   self)
Get number of weak sub-models.

Returns
-------
num_trees : int
    The number of weak sub-models.

◆ predict()

lightgbm.basic.Booster.predict (   self,
  data,
  num_iteration = None,
  raw_score = False,
  pred_leaf = False,
  pred_contrib = False,
  data_has_header = False,
  is_reshape = True,
**  kwargs 
)
Make a prediction.

Parameters
----------
data : string, numpy array, pandas DataFrame, H2O DataTable or scipy.sparse
    Data source for prediction.
    If string, it represents the path to txt file.
num_iteration : int or None, optional (default=None)
    Limit number of iterations in the prediction.
    If None, if the best iteration exists, it is used; otherwise, all iterations are used.
    If <= 0, all iterations are used (no limits).
raw_score : bool, optional (default=False)
    Whether to predict raw scores.
pred_leaf : bool, optional (default=False)
    Whether to predict leaf index.
pred_contrib : bool, optional (default=False)
    Whether to predict feature contributions.

    Note
    ----
    If you want to get more explanation for your model's predictions using SHAP values
    like SHAP interaction values,
    you can install shap package (https://github.com/slundberg/shap).

data_has_header : bool, optional (default=False)
    Whether the data has header.
    Used only if data is string.
is_reshape : bool, optional (default=True)
    If True, result is reshaped to [nrow, ncol].
**kwargs
    Other parameters for the prediction.

Returns
-------
result : numpy array
    Prediction result.

◆ refit()

lightgbm.basic.Booster.refit (   self,
  data,
  label,
  decay_rate = 0.9,
**  kwargs 
)
Refit the existing Booster by new data.

Parameters
----------
data : string, numpy array, pandas DataFrame, H2O DataTable or scipy.sparse
    Data source for refit.
    If string, it represents the path to txt file.
label : list, numpy 1-D array or pandas Series / one-column DataFrame
    Label for refit.
decay_rate : float, optional (default=0.9)
    Decay rate of refit,
    will use ``leaf_output = decay_rate * old_leaf_output + (1.0 - decay_rate) * new_leaf_output`` to refit trees.
**kwargs
    Other parameters for refit.
    These parameters will be passed to ``predict`` method.

Returns
-------
result : Booster
    Refitted Booster.

◆ reset_parameter()

lightgbm.basic.Booster.reset_parameter (   self,
  params 
)
Reset parameters of Booster.

Parameters
----------
params : dict
    New parameters for Booster.

Returns
-------
self : Booster
    Booster with new parameters.

◆ rollback_one_iter()

lightgbm.basic.Booster.rollback_one_iter (   self)
Rollback one iteration.

Returns
-------
self : Booster
    Booster with rolled back one iteration.

◆ save_model()

lightgbm.basic.Booster.save_model (   self,
  filename,
  num_iteration = None,
  start_iteration = 0 
)
Save Booster to file.

Parameters
----------
filename : string
    Filename to save Booster.
num_iteration : int or None, optional (default=None)
    Index of the iteration that should be saved.
    If None, if the best iteration exists, it is saved; otherwise, all iterations are saved.
    If <= 0, all iterations are saved.
start_iteration : int, optional (default=0)
    Start index of the iteration that should be saved.

Returns
-------
self : Booster
    Returns self.

◆ set_attr()

lightgbm.basic.Booster.set_attr (   self,
**  kwargs 
)
Set attributes to the Booster.

Parameters
----------
**kwargs
    The attributes to set.
    Setting a value to None deletes an attribute.

Returns
-------
self : Booster
    Booster with set attributes.

◆ set_network()

lightgbm.basic.Booster.set_network (   self,
  machines,
  local_listen_port = 12400,
  listen_time_out = 120,
  num_machines = 1 
)
Set the network configuration.

Parameters
----------
machines : list, set or string
    Names of machines.
local_listen_port : int, optional (default=12400)
    TCP listen port for local machines.
listen_time_out : int, optional (default=120)
    Socket time-out in minutes.
num_machines : int, optional (default=1)
    The number of machines for parallel learning application.

Returns
-------
self : Booster
    Booster with set network.

◆ set_train_data_name()

lightgbm.basic.Booster.set_train_data_name (   self,
  name 
)
Set the name to the training Dataset.

Parameters
----------
name : string
    Name for the training Dataset.

Returns
-------
self : Booster
    Booster with set training Dataset name.

◆ shuffle_models()

lightgbm.basic.Booster.shuffle_models (   self,
  start_iteration = 0,
  end_iteration = -1 
)
Shuffle models.

Parameters
----------
start_iteration : int, optional (default=0)
    The first iteration that will be shuffled.
end_iteration : int, optional (default=-1)
    The last iteration that will be shuffled.
    If <= 0, means the last available iteration.

Returns
-------
self : Booster
    Booster with shuffled models.

◆ update()

lightgbm.basic.Booster.update (   self,
  train_set = None,
  fobj = None 
)
Update Booster for one iteration.

Parameters
----------
train_set : Dataset or None, optional (default=None)
    Training data.
    If None, last training data is used.
fobj : callable or None, optional (default=None)
    Customized objective function.

    For multi-class task, the score is group by class_id first, then group by row_id.
    If you want to get i-th row score in j-th class, the access way is score[j * num_data + i]
    and you should group grad and hess in this way as well.

Returns
-------
is_finished : bool
    Whether the update was successfully finished.

The documentation for this class was generated from the following file: