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

如何获取VotingClassifier对象中每个基模型的准确率等评估指标

实现方案

首先你需要先拆分训练集和测试集,避免用训练数据计算指标导致结果过拟合。训练完成的VotingClassifier会通过estimators_属性暴露所有已训练的基模型,直接遍历该属性计算指标即可,完整代码如下:

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import VotingClassifier
import lightgbm as lgb
import xgboost as xgb

# 拆分数据集,测试集占比30%可自行调整
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 定义模型
lr_model = LogisticRegression()
lgb_model = lgb.LGBMClassifier()
xgb_model = xgb.XGBClassifier()
model = VotingClassifier(
    estimators=[("lr", lr_model), ("lgbm", lgb_model), ("xgb", xgb_model)],
    voting='soft'
)

# 训练集成模型
model.fit(X_train, y_train)

# 遍历基模型计算并打印准确率
for model_name, estimator in model.estimators_:
    pred = estimator.predict(X_test)
    accuracy = accuracy_score(y_test, pred)
    print(f"- {model_name}_model accuracy => {accuracy:.2f}")

补充说明

  • 输出结果的顺序和你传入estimators参数的模型顺序完全一致
  • 若需要计算其他评估指标(如精确率、召回率、F1值、AUC等),替换accuracy_score为sklearn.metrics下对应的指标函数即可
  • 保留小数位数可以通过修改.2f的参数调整,比如要保留三位小数就改为.3f

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 10:57:03