You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取LightGBM中每棵树的单独预测结果?

获取LightGBM多分类中每棵树的预测结果及理解predict_proba的概率计算

一、获取每棵树的单独预测得分

LightGBM多分类任务(objective='multiclass')中,每棵树会针对单个类别输出得分增量,所有树的得分累加后再通过softmax得到最终概率。以下是两种实用的获取单棵树得分的方法:

方法1:通过迭代num_iteration计算得分增量

这种方法通过对比不同迭代次数的累计得分,直接得到每棵树的贡献:

import lightgbm as lgb
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# 1. 训练多分类模型
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_classes=3, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = lgb.LGBMClassifier(
    objective='multiclass',
    num_class=3,
    n_estimators=10,
    random_state=42
)
model.fit(X_train, y_train)

# 2. 计算每棵树的得分增量
tree_raw_scores = []
# 第一棵树的得分
first_tree_scores = model.predict(X_test, raw_score=True, num_iteration=1)
tree_raw_scores.append(first_tree_scores)

# 后续每棵树的得分 = 当前迭代累计得分 - 上一轮迭代累计得分
for i in range(2, model.n_estimators + 1):
    prev_total = model.predict(X_test, raw_score=True, num_iteration=i-1)
    curr_total = model.predict(X_test, raw_score=True, num_iteration=i)
    tree_scores = curr_total - prev_total
    tree_raw_scores.append(tree_scores)

# 验证:所有树得分之和等于总原始得分
total_raw_scores = model.predict(X_test, raw_score=True)
assert np.allclose(np.sum(tree_raw_scores, axis=0), total_raw_scores)

方法2:通过树结构和叶子索引获取得分

这种方法直接解析树的叶子节点权重,结合样本落在的叶子索引计算得分:

# 1. 获取每个样本在每棵树上的叶子索引
leaf_indices = model.predict(X_test, pred_leaf=True)

# 2. 获取所有树的结构数据
tree_df = model.booster_.trees_to_dataframe()

tree_raw_scores_alt = []
for tree_idx in range(model.n_estimators):
    # 提取当前树的信息
    current_tree = tree_df[tree_df['tree_index'] == tree_idx]
    # 获取叶子节点的权重映射
    leaf_weights = current_tree[current_tree['node_type'] == 'leaf'].set_index('leaf_index')['value'].to_dict()
    # 获取当前树对应的类别(多分类下每棵树只对应一个类别)
    tree_class = current_tree['class'].iloc[0]
    
    # 初始化得分数组,仅对应类别有值
    tree_scores = np.zeros((X_test.shape[0], model.num_class_))
    tree_scores[:, tree_class] = [leaf_weights[leaf] for leaf in leaf_indices[:, tree_idx]]
    tree_raw_scores_alt.append(tree_scores)

# 验证结果一致性
assert np.allclose(np.sum(tree_raw_scores_alt, axis=0), total_raw_scores)

二、理解predict_proba的概率计算逻辑

LightGBM的predict_proba本质是两步计算:

  1. 累加所有树的得分:每个类别得到一个累计的原始得分(logit值)
  2. Softmax转换:对每个样本的所有类别得分做softmax,将其转换为概率(总和为1)

手动实现验证:

def softmax(x):
    # 减去最大值防止数值溢出
    exp_vals = np.exp(x - np.max(x, axis=1, keepdims=True))
    return exp_vals / np.sum(exp_vals, axis=1, keepdims=True)

# 手动计算概率
manual_proba = softmax(total_raw_scores)
# 和模型输出对比
model_proba = model.predict_proba(X_test)
assert np.allclose(manual_proba, model_proba)

关键说明

  • 多分类下,每棵树仅对单个类别产生得分增量,其他类别的增量为0
  • 原始得分(raw score)是未经过归一化的累计值,数值范围无限制
  • Softmax转换确保所有类别的概率之和为1,符合概率分布的要求

内容的提问来源于stack exchange,提问作者mftgk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 20:05:06