0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

XGBoostの説明変数の重要度可視化

Posted at

#内容
クラスラベルのエンコーディング
説明変数の重要度の可視化

#コード

qiita.py
import pandas as pd
from sklearn import datasets
from xgboost import XGBClassifier
from xgboost import plot_importance
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

iris=datasets.load_iris()
df=pd.DataFrame(iris.data, columns=iris.feature_names)
df['target']=iris.target_names[iris.target]

#クラスラベルのエンコーディング
class_mapping={label:idx for idx, label in enumerate(np.unique(df['target']))}
df['target']=df['target'].map(class_mapping)

#データの分割
y=df['target']
features=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
X=df[features]

train_X,val_X,train_y,val_y=train_test_split(X,y,test_size=0.3)


#学習
param = {'max_depth': 2, 'eta': 1, 'objective': 'multi:softmax', 'num_class': 3}
xgb_model = XGBClassifier(param)
xgb_model.fit(train_X, 
              train_y)

#可視化
plt.rcParams["figure.figsize"] = (15, 6)
plot_importance(xgb_model)
plt.show()

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?