如何使用 xgboost.XGBCClassifier.feature_importances_ 模型绘图? (Matplotlib)
pythonserver side programmingprogramming更新于 2025/8/8 13:32:17
要更改 xgboost.plot_importance 中绘图的大小,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 从 csv 文件加载数据。
- 从加载的数据集中获取 x 和 y 数据。
- 获取 xgboost.XGBCClassifier.feature_importances_ model 实例。
- 将 x 和 y 数据拟合到模型中。
- 打印模型。
- 制作条形图。
- 要显示图形,请使用 show()方法。
示例
from numpy import loadtxt
from xgboost import XGBClassifier
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# data.csv 包含如下数据 -> 13, 145, 82, 19, 110, 22.2, 0.245, 57, 0
dataset = loadtxt('data.csv', delimiter=",")
X = dataset[:, 0:8]
y = dataset[:, 8]
model = XGBClassifier()
model.fit(X, y)
print(model.feature_importances_)
plt.bar(range(len(model.feature_importances_)), model.feature_importances_)
plt.show()
输出
[13:46:53] WARNING: ../src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior. [0.10621197 0.2424023 0.08803366 0.07818192 0.10381887 0.1486732 0.10059207 0.13208601]

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

