2016-06-07 90 views
3

如何訪問python/R中的xgboost模型的單個樹?如何訪問Python中的xgboost模型的單個樹/ R

下面我從隨機森林從sklearn樹。

估計= RandomForestRegressor(oob_score =真,n_estimators = 10,max_features = '自動')estimator.fit(tarning_data,traning_target)樹1 = estimator.estimators_ [0] = leftChild tree1.tree_.children_left
rightChild =樹1 .tree_.children_right

+0

我也想回答這個問題,因爲它是必要的置信區間。我知道,一旦你已經訓練升壓模式'bst',只需撥打 bst.predict(數據,pred_leaf = TRUE) 輸出將是'(N_SAMPLES次,n_estimators)'矩陣與每個記錄指示預測每棵樹的每個樣本的葉索引,但不知道如何恢復每棵樹的實際預測。 – michel

+0

你們知道了嗎? – None

回答

0

你想檢查樹木嗎?

在Python中,你可以轉儲樹木字符串列表:

m = xgb.XGBClassifier(max_depth=2, n_estimators=3).fit(X, y) 
m.get_booster().get_dump() 

>

['0:[sincelastrun<23.2917] yes=1,no=2,missing=2\n\t1:[sincelastrun<18.0417] yes=3,no=4,missing=4\n\t\t3:leaf=-0.0965415\n\t\t4:leaf=-0.0679503\n\t2:[sincelastrun<695.025] yes=5,no=6,missing=6\n\t\t5:leaf=-0.0992546\n\t\t6:leaf=-0.0984374\n', 
'0:[sincelastrun<23.2917] yes=1,no=2,missing=2\n\t1:[sincelastrun<16.8917] yes=3,no=4,missing=4\n\t\t3:leaf=-0.0928132\n\t\t4:leaf=-0.0676056\n\t2:[sincelastrun<695.025] yes=5,no=6,missing=6\n\t\t5:leaf=-0.0945284\n\t\t6:leaf=-0.0937463\n', 
'0:[sincelastrun<23.2917] yes=1,no=2,missing=2\n\t1:[sincelastrun<18.175] yes=3,no=4,missing=4\n\t\t3:leaf=-0.0878571\n\t\t4:leaf=-0.0610089\n\t2:[sincelastrun<695.025] yes=5,no=6,missing=6\n\t\t5:leaf=-0.0904395\n\t\t6:leaf=-0.0896808\n'] 

或者其轉儲到一個文件中(漂亮格式):

m.get_booster().dump_model("out.txt") 

>

booster[0]: 
0:[sincelastrun<23.2917] yes=1,no=2,missing=2 
    1:[sincelastrun<18.0417] yes=3,no=4,missing=4 
     3:leaf=-0.0965415 
     4:leaf=-0.0679503 
    2:[sincelastrun<695.025] yes=5,no=6,missing=6 
     5:leaf=-0.0992546 
     6:leaf=-0.0984374 
booster[1]: 
0:[sincelastrun<23.2917] yes=1,no=2,missing=2 
    1:[sincelastrun<16.8917] yes=3,no=4,missing=4 
     3:leaf=-0.0928132 
     4:leaf=-0.0676056 
    2:[sincelastrun<695.025] yes=5,no=6,missing=6 
     5:leaf=-0.0945284 
     6:leaf=-0.0937463 
booster[2]: 
0:[sincelastrun<23.2917] yes=1,no=2,missing=2 
    1:[sincelastrun<18.175] yes=3,no=4,missing=4 
     3:leaf=-0.0878571 
     4:leaf=-0.0610089 
    2:[sincelastrun<695.025] yes=5,no=6,missing=6 
     5:leaf=-0.0904395 
     6:leaf=-0.0896808