LightGBM中的predict_proba()函数是如何内部工作的?

这是关于理解LightGBM内部如何预测类别的概率的问题,涉及到LightGBM的使用。

其他软件包,如sklearn,对其分类器提供了详细的说明。例如:

概率估计值。

所有类别的返回估计值按类别标签排序。

对于多类别问题,如果multi_class设置为“multinomial”,则使用softmax函数来计算每个类别的预测概率。否则,使用一对多方法,即使用逻辑函数计算每个类别的概率,并在所有类别中对这些值进行归一化处理。

预测X的类别概率。

输入样本的预测类别概率计算为森林中各树的平均预测类别概率。单棵树的类别概率是叶子节点中同类样本的比例。

还有其他Stack Overflow的问题提供了更多细节,例如关于:

我试图揭示LightGBM的predict_proba函数的相同细节。文档没有列出概率计算的具体细节。

文档仅简单地说明:

返回每个样本每个类别的预测概率。

源代码如下:

def predict_proba(self, X, raw_score=False, start_iteration=0, num_iteration=None,                  pred_leaf=False, pred_contrib=False, **kwargs):    """Return the predicted probability for each class 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.    start_iteration : int, optional (default=0)        Start index of the iteration to predict.        If <= 0, starts from the first iteration.    num_iteration : int or None, optional (default=None)        Total number of iterations used in the prediction.        If None, if the best iteration exists and start_iteration <= 0, the best iteration is used;        otherwise, all iterations from ``start_iteration`` are used (no limits).        If <= 0, all iterations from ``start_iteration`` 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 explanations for your model's predictions using SHAP values,            like SHAP interaction values,            you can install the shap package (https://github.com/slundberg/shap).            Note that unlike the shap package, with ``pred_contrib`` we return a matrix with an extra            column, where the last column is the expected value.    **kwargs        Other parameters for the prediction.    Returns    -------    predicted_probability : array-like of shape = [n_samples, n_classes]        The predicted probability for each class for each sample.    X_leaves : array-like of shape = [n_samples, n_trees * n_classes]        If ``pred_leaf=True``, the predicted leaf of every tree for each sample.    X_SHAP_values : array-like of shape = [n_samples, (n_features + 1) * n_classes] or list with n_classes length of such objects        If ``pred_contrib=True``, the feature contributions for each sample.    """    result = super(LGBMClassifier, self).predict(X, raw_score, start_iteration, num_iteration,                                                 pred_leaf, pred_contrib, **kwargs)    if callable(self._objective) and not (raw_score or pred_leaf or pred_contrib):        warnings.warn("Cannot compute class probabilities or labels "                      "due to the usage of customized objective function.\n"                      "Returning raw scores instead.")        return result    elif self._n_classes > 2 or raw_score or pred_leaf or pred_contrib:        return result    else:        return np.vstack((1. - result, result)).transpose()

我如何理解LightGBMpredict_proba函数的内部工作原理?


回答:

LightGBM,作为所有用于分类的梯度提升方法一样,本质上是结合了决策树和逻辑回归。我们从表示概率的相同逻辑函数(也称为softmax)开始:

P(y = 1 | X) = 1/(1 + exp(Xw))

有趣的是,特征矩阵X是由决策树集合的终端节点组成。这些节点随后由w加权,这是一个必须学习的参数。用于学习权重的机制取决于所使用的精确学习算法。同样,X的构建也依赖于算法。例如,LightGBM引入了两个新颖的特性,这些特性使其在性能上超过了XGBoost:“基于梯度的单边采样”和”独占特征捆绑”。一般来说,每行收集每个样本的终端叶子,列代表终端叶子。

所以文档可以这样说…

概率估计值。

输入样本的预测类别概率计算为提供样本的决策树集合的加权终端叶子的softmax值。

要了解更多细节,你需要深入研究提升、XGBoost以及最终LightGBM的论文,但鉴于你提供的其他文档示例,这似乎有点过分了。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注