Computer >> Máy Tính >  >> Lập trình >> Python

Làm thế nào để vẽ với mô hình xgboost.XGBCClassifier.feature_importances_? (Matplotlib)


Để thay đổi kích thước của ô trong xgboost.plot_importance, chúng ta có thể thực hiện các bước sau -

  • Đặt kích thước hình và điều chỉnh phần đệm giữa và xung quanh các ô phụ.
  • Tải dữ liệu từ csv tệp.
  • Nhận x y dữ liệu từ tập dữ liệu đã tải.
  • Nhận xgboost.XGBCClassifier.feature_importances_ model ví dụ.
  • Vừa với x y dữ liệu vào mô hình.
  • In mô hình.
  • Tạo một biểu đồ thanh.
  • Để hiển thị hình này, hãy sử dụng show () phương pháp.

Ví dụ

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 contains data like -> 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()

Đầu ra

[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]

Làm thế nào để vẽ với mô hình xgboost.XGBCClassifier.feature_importances_? (Matplotlib)