How to visualize feature importance per class?
library(caret)
library(dplyr)
library(xgboost)
library(Hmisc)
library(factoextra)
library(NbClust)
library(ggplot2)
library(ggcorrplot)
library(ggpubr)
library(gridExtra)
library(fastDummies)
library(smotefamily)
library(SHAPforxgboost)
x_train_xgb<-xgb.DMatrix(as.matrix(x_train),label=y_train)
x_test_xgb<-xgb.DMatrix(as.matrix(x_test),label=y_test)
params_xgb<-list(booster = "dart",
objective = "binary:logistic",eta=0.3,gamma=1,max_depth=5,
min_child_weight=2,subsample=1,colsample_bytree=1,lambda=1.25,alpha=0.75)
xgb_cv<-xgb.cv(params=params_xgb,
data=x_train_xgb,nrounds=600,nfold=5,showsd = T,
early.stop.round = 35, maximize = F,metrics=c('auc'))
gb_dt <- xgb.train(params = params_xgb,
data = x_train_xgb,
nrounds = xgb_cv$best_iteration,
print_every_n = 2,
eval_metric=c('auc'),
watchlist=list(train=x_train_xgb,eval=x_test_xgb))
xgb_pred<-predict(gb_dt,x_test_xgb)
binary_pred_xgb<-as.numeric(xgb_pred > 0.5)
confusionMatrix(as.factor(binary_pred_xgb),as.factor(y_test))
##Xgboost featuring importance
xgb_contrib<-shap.values(gb_dt,as.matrix(x_train))
shap.
shap_value_xgb<-xgb_contrib$shap_score
shap_long_xgb<-shap.prep(shap_contrib = shap_value_xgb,
X_train = as.matrix(x_test),
var_cat=as.matrix(as.facreference_data))[![enter image description here][1]][1]
important_var<-shap.importance(shap_long_xgb,top_n=10)
important_var<-important_var%>%dplyr::select(variable,mean_abs_shap)%>%
mutate(percentage=round(mean_abs_shap*100,2))
ggplot(important_var)+geom_bar(aes(x=variable,y=percentage),stat='identity')+
scale_x_discrete(guide=guide_axis(angle=90))+
geom_text(aes(label=paste(percentage,"%"),x=variable,y=percentage+1.5))+
ggtitle('Top 10 important variable')
Here it is for plotting data
According to the plotting data, there isn't indicate per class (which we can't interpret each feature to binary class).
Is there any way?
