Medial Code Documentation
|
Public Member Functions | |
__init__ (self, boosting_type='gbdt', num_leaves=31, max_depth=-1, learning_rate=0.1, n_estimators=100, subsample_for_bin=200000, objective=None, class_weight=None, min_split_gain=0., min_child_weight=1e-3, min_child_samples=20, subsample=1., subsample_freq=0, colsample_bytree=1., reg_alpha=0., reg_lambda=0., random_state=None, n_jobs=-1, silent=True, importance_type='split', **kwargs) | |
get_params (self, deep=True) | |
set_params (self, **params) | |
fit (self, X, y, sample_weight=None, init_score=None, group=None, eval_set=None, eval_names=None, eval_sample_weight=None, eval_class_weight=None, eval_init_score=None, eval_group=None, eval_metric=None, early_stopping_rounds=None, verbose=True, feature_name='auto', categorical_feature='auto', callbacks=None) | |
predict (self, X, raw_score=False, num_iteration=None, pred_leaf=False, pred_contrib=False, **kwargs) | |
n_features_ (self) | |
best_score_ (self) | |
best_iteration_ (self) | |
objective_ (self) | |
booster_ (self) | |
evals_result_ (self) | |
feature_importances_ (self) | |
Protected Attributes | |
_Booster | |
_evals_result | |
_best_score | |
_best_iteration | |
_other_params | |
_objective | |
_n_features | |
_classes | |
_n_classes | |
_fobj | |
Implementation of the scikit-learn API for LightGBM.
lightgbm.sklearn.LGBMModel.__init__ | ( | self, | |
boosting_type = 'gbdt' , |
|||
num_leaves = 31 , |
|||
max_depth = -1 , |
|||
learning_rate = 0.1 , |
|||
n_estimators = 100 , |
|||
subsample_for_bin = 200000 , |
|||
objective = None , |
|||
class_weight = None , |
|||
min_split_gain = 0. , |
|||
min_child_weight = 1e-3 , |
|||
min_child_samples = 20 , |
|||
subsample = 1. , |
|||
subsample_freq = 0 , |
|||
colsample_bytree = 1. , |
|||
reg_alpha = 0. , |
|||
reg_lambda = 0. , |
|||
random_state = None , |
|||
n_jobs = -1 , |
|||
silent = True , |
|||
importance_type = 'split' , |
|||
** | kwargs | ||
) |
Construct a gradient boosting model. Parameters ---------- boosting_type : string, optional (default='gbdt') 'gbdt', traditional Gradient Boosting Decision Tree. 'dart', Dropouts meet Multiple Additive Regression Trees. 'goss', Gradient-based One-Side Sampling. 'rf', Random Forest. num_leaves : int, optional (default=31) Maximum tree leaves for base learners. max_depth : int, optional (default=-1) Maximum tree depth for base learners, -1 means no limit. learning_rate : float, optional (default=0.1) Boosting learning rate. You can use ``callbacks`` parameter of ``fit`` method to shrink/adapt learning rate in training using ``reset_parameter`` callback. Note, that this will ignore the ``learning_rate`` argument in training. n_estimators : int, optional (default=100) Number of boosted trees to fit. subsample_for_bin : int, optional (default=200000) Number of samples for constructing bins. objective : string, callable or None, optional (default=None) Specify the learning task and the corresponding learning objective or a custom objective function to be used (see note below). Default: 'regression' for LGBMRegressor, 'binary' or 'multiclass' for LGBMClassifier, 'lambdarank' for LGBMRanker. class_weight : dict, 'balanced' or None, optional (default=None) Weights associated with classes in the form ``{class_label: weight}``. Use this parameter only for multi-class classification task; for binary classification task you may use ``is_unbalance`` or ``scale_pos_weight`` parameters. The 'balanced' mode uses the values of y to automatically adjust weights inversely proportional to class frequencies in the input data as ``n_samples / (n_classes * np.bincount(y))``. If None, all classes are supposed to have weight one. Note, that these weights will be multiplied with ``sample_weight`` (passed through the ``fit`` method) if ``sample_weight`` is specified. min_split_gain : float, optional (default=0.) Minimum loss reduction required to make a further partition on a leaf node of the tree. min_child_weight : float, optional (default=1e-3) Minimum sum of instance weight (hessian) needed in a child (leaf). min_child_samples : int, optional (default=20) Minimum number of data needed in a child (leaf). subsample : float, optional (default=1.) Subsample ratio of the training instance. subsample_freq : int, optional (default=0) Frequence of subsample, <=0 means no enable. colsample_bytree : float, optional (default=1.) Subsample ratio of columns when constructing each tree. reg_alpha : float, optional (default=0.) L1 regularization term on weights. reg_lambda : float, optional (default=0.) L2 regularization term on weights. random_state : int or None, optional (default=None) Random number seed. If None, default seeds in C++ code will be used. n_jobs : int, optional (default=-1) Number of parallel threads. silent : bool, optional (default=True) Whether to print messages while running boosting. importance_type : string, optional (default='split') The type of feature importance to be filled into ``feature_importances_``. 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. **kwargs Other parameters for the model. Check http://lightgbm.readthedocs.io/en/latest/Parameters.html for more parameters. Note ---- \*\*kwargs is not supported in sklearn, it may cause unexpected issues. Attributes ---------- n_features_ : int The number of features of fitted model. classes_ : array of shape = [n_classes] The class label array (only for classification problem). n_classes_ : int The number of classes (only for classification problem). best_score_ : dict or None The best score of fitted model. best_iteration_ : int or None The best iteration of fitted model if ``early_stopping_rounds`` has been specified. objective_ : string or callable The concrete objective used while fitting this model. booster_ : Booster The underlying Booster of this model. evals_result_ : dict or None The evaluation results if ``early_stopping_rounds`` has been specified. feature_importances_ : array of shape = [n_features] The feature importances (the higher, the more important the feature). Note ---- A custom objective function can be provided for the ``objective`` parameter. In this case, it should have the signature ``objective(y_true, y_pred) -> grad, hess`` or ``objective(y_true, y_pred, group) -> 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. group : array-like Group/query data, used for ranking task. grad : array-like of shape = [n_samples] or shape = [n_samples * n_classes] (for multi-class task) The value of the gradient 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 derivative for each sample point. For multi-class task, the y_pred is group by class_id first, then group by row_id. If you want to get i-th row y_pred in j-th class, the access way is y_pred[j * num_data + i] and you should group grad and hess in this way as well.
lightgbm.sklearn.LGBMModel.best_iteration_ | ( | self | ) |
Get the best iteration of fitted model.
lightgbm.sklearn.LGBMModel.best_score_ | ( | self | ) |
Get the best score of fitted model.
lightgbm.sklearn.LGBMModel.booster_ | ( | self | ) |
Get the underlying lightgbm Booster of this model.
lightgbm.sklearn.LGBMModel.evals_result_ | ( | self | ) |
Get the evaluation results.
lightgbm.sklearn.LGBMModel.feature_importances_ | ( | self | ) |
Get feature importances. Note ---- Feature importance in sklearn interface used to normalize to 1, it's deprecated after 2.0.4 and is the same as Booster.feature_importance() now. ``importance_type`` attribute is passed to the function to configure the type of importance values to be extracted.
lightgbm.sklearn.LGBMModel.fit | ( | self, | |
X, | |||
y, | |||
sample_weight = None , |
|||
init_score = None , |
|||
group = None , |
|||
eval_set = None , |
|||
eval_names = None , |
|||
eval_sample_weight = None , |
|||
eval_class_weight = None , |
|||
eval_init_score = None , |
|||
eval_group = None , |
|||
eval_metric = None , |
|||
early_stopping_rounds = None , |
|||
verbose = True , |
|||
feature_name = 'auto' , |
|||
categorical_feature = 'auto' , |
|||
callbacks = None |
|||
) |
Build a gradient boosting model from the training set (X, y). Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] Input feature matrix. y : array-like of shape = [n_samples] The target values (class labels in classification, real numbers in regression). sample_weight : array-like of shape = [n_samples] or None, optional (default=None) Weights of training data. init_score : array-like of shape = [n_samples] or None, optional (default=None) Init score of training data. group : array-like or None, optional (default=None) Group data of training data. eval_set : list or None, optional (default=None) A list of (X, y) tuple pairs to use as validation sets. eval_names : list of strings or None, optional (default=None) Names of eval_set. eval_sample_weight : list of arrays or None, optional (default=None) Weights of eval data. eval_class_weight : list or None, optional (default=None) Class weights of eval data. eval_init_score : list of arrays or None, optional (default=None) Init score of eval data. eval_group : list of arrays or None, optional (default=None) Group data of eval data. eval_metric : string, list of strings, callable or None, optional (default=None) If string, it should be a built-in evaluation metric to use. If callable, it should be a custom evaluation metric, see note below for more details. In either case, the ``metric`` from the model parameters will be evaluated and used as well. Default: 'l2' for LGBMRegressor, 'logloss' for LGBMClassifier, 'ndcg' for LGBMRanker. early_stopping_rounds : int or None, optional (default=None) Activates early stopping. The model will train until the validation score stops improving. Validation score needs to improve at least every ``early_stopping_rounds`` round(s) to continue training. Requires at least one validation data and one metric. If there's more than one, will check all of them. But the training data is ignored anyway. verbose : bool or int, optional (default=True) Requires at least one evaluation data. If True, the eval metric on the eval set is printed at each boosting stage. If int, the eval metric on the eval set is printed at every ``verbose`` boosting stage. The last boosting stage or the boosting stage found by using ``early_stopping_rounds`` is also printed. Example ------- With ``verbose`` = 4 and at least one item in ``eval_set``, an evaluation metric is printed every 4 (instead of 1) boosting stages. feature_name : list of strings or 'auto', optional (default='auto') Feature names. If 'auto' and data is pandas DataFrame, data columns names are used. categorical_feature : list of strings or int, or 'auto', optional (default='auto') Categorical features. If list of int, interpreted as indices. If list of strings, interpreted as feature names (need to specify ``feature_name`` as well). If 'auto' and data is pandas DataFrame, pandas categorical columns are used. All values in categorical features should be less than int32 max value (2147483647). Large values could be memory consuming. Consider using consecutive integers starting from zero. All negative values in categorical features will be treated as missing values. callbacks : list of callback functions or None, optional (default=None) List of callback functions that are applied at each iteration. See Callbacks in Python API for more information. Returns ------- self : object Returns self. Note ---- Custom eval function 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_bigger_better) or list of (eval_name, eval_result, is_bigger_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. weight : array-like of shape = [n_samples] The weight of samples. group : array-like Group/query data, used for ranking task. eval_name : string The name of evaluation. eval_result : float The eval result. is_bigger_better : bool Is eval result bigger better, e.g. AUC is bigger_better. For multi-class task, the y_pred is group by class_id first, then group by row_id. If you want to get i-th row y_pred in j-th class, the access way is y_pred[j * num_data + i].
Reimplemented in lightgbm.sklearn.LGBMClassifier, lightgbm.sklearn.LGBMRegressor, and lightgbm.sklearn.LGBMRanker.
lightgbm.sklearn.LGBMModel.get_params | ( | self, | |
deep = True |
|||
) |
Get parameters for this estimator. Parameters ---------- deep : bool, optional (default=True) If True, will return the parameters for this estimator and contained subobjects that are estimators. Returns ------- params : dict Parameter names mapped to their values.
lightgbm.sklearn.LGBMModel.n_features_ | ( | self | ) |
Get the number of features of fitted model.
lightgbm.sklearn.LGBMModel.objective_ | ( | self | ) |
Get the concrete objective used while fitting this model.
lightgbm.sklearn.LGBMModel.predict | ( | self, | |
X, | |||
raw_score = False , |
|||
num_iteration = None , |
|||
pred_leaf = False , |
|||
pred_contrib = False , |
|||
** | kwargs | ||
) |
Return the predicted value for each sample. Parameters ---------- X : array-like or sparse matrix of shape = [n_samples, n_features] Input features matrix. raw_score : bool, optional (default=False) Whether to predict raw scores. 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 trees are used. If <= 0, all trees are used (no limits). 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). **kwargs Other parameters for the prediction. Returns ------- predicted_result : array-like of shape = [n_samples] or shape = [n_samples, n_classes] The predicted values. X_leaves : array-like of shape = [n_samples, n_trees] or shape [n_samples, n_trees * n_classes] If ``pred_leaf=True``, the predicted leaf every tree for each sample. X_SHAP_values : array-like of shape = [n_samples, n_features + 1] or shape [n_samples, (n_features + 1) * n_classes] If ``pred_contrib=True``, the each feature contributions for each sample.
Reimplemented in lightgbm.sklearn.LGBMClassifier.
lightgbm.sklearn.LGBMModel.set_params | ( | self, | |
** | params | ||
) |
Set the parameters of this estimator. Parameters ---------- **params Parameter names with their new values. Returns ------- self : object Returns self.