human readable rules from xgboost in R

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 03:31:24

问题


I try to use xgboost in R to get rules (gbtree) from my data, so I can use the rules in an other system (not predicted data with 'predict'). The Input-Data have appr. 1500 colums and 40 Mio rows with binary, sparse data and the Label is a binary column, too.

library(xgboost)
library(Matrix)

labels <- data.frame(labels = sample.int(2, m*1, TRUE)-1L)
observations <- Matrix(as.matrix(data.frame(feat_01=sample.int(2, size=100, T) -1,
                                            feat_02=sample.int(2, size=100, T) -1,
                                            feat_03=sample.int(2, size=100, T) -1,
                                            feat_04=sample.int(2, size=100, T) -1,
                                            feat_05=sample.int(2, size=100, T) -1)), sparse=T)

dtrain <- xgb.DMatrix(data = observations, label = labels$labels)

bstResult <- xgb.train(data = dtrain, 
                       nthread = 1, 
                       nround = 4, 
                       max_depth = 3, 
                       verbose = T,
                       objective = "binary:logistic",
                       booster='gbtree')

xgb.dump(bstResult)
xgb.plot.tree(model = bstResult, n_first_tree = 2)

I visualize the data as xgb.dump or xgb.plot.tree. But I need the data in a form like:

rule1: feat_01 == 1 & feat_02==1 & feat_03== 0 --> Label = 1

rule2: feat_01== 0 & feat_03==1 & feat_04== 1 --> Label = 0

Is this possible or am I on the wrong track?

Regards Heiko

edit: added example and tried to make the question better


回答1:


On one hand, I think you can use the importance matrix to obtain the coverage and ranking of each feature. On the other hand, xgboost uses an ensemble of weak learners using bagging, the rules should be 'rare'



来源:https://stackoverflow.com/questions/42698231/human-readable-rules-from-xgboost-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!