
http://www.ma-xy.com
第一章 决策树和集成学习 1.2 集成学习
94 # setup parameters f o r xgboost
95 param = {}
96 # use softmax multi−c l a s s c l a s s i f i c a t i o n
97 param [ ’ o b j e c t i v e ’ ] = ’ multi : softmax ’
98 # s c a l e weight of p o s i t i v e examples
99 param [ ’ et a ’ ] = 0. 1
100 param [ ’max_depth ’ ] = 6
101 param [ ’ s i l e n t ’ ] = 1
102 param [ ’ nthread ’ ] = 4
103 param [ ’ num_class ’ ] = 6
104
105 w a t c h list = [ ( xg_train , ’ t r a i n ’ ) , ( xg_test , ’ t e s t ’ ) ]
106 num_round = 5
107 bst = xgb . t r a i n (param , xg_train , num_round, w a t c hlist )
108 # get p r edicti o n
109 pred = b st . p redi c t ( xg_test )
110 error _r ate = np . sum( pred != test_Y ) / test_Y . shape [ 0 ]
111 p r int ( ’ Test error using softmax = {} ’ . format ( err or_rate ) )
112
113 # do the same thing again , but output p r o b a b i l i t i e s
114 param [ ’ o b j e c t i v e ’ ] = ’ multi : s oft pro b ’
115 bst = xgb . t r a i n (param , xg_train , num_round, w a t c hlist )
116 # Note : t h i s convention has been changed s i n c e xgboost−unity
117 # get pre dict ion , t h i s i s in 1D array , need reshape to ( ndata , nclass )
118 pred_prob = b st . p redi c t ( xg_test ) . reshape ( test_Y . shape [ 0 ] , 6)
119 pred_label = np . argmax ( pred_prob , axi s =1)
120 error _r ate = np . sum( pred_label != test_Y ) / test_Y . shape [ 0 ]
121 p r int ( ’ Test error using soft pro b = {} ’ . format ( error_ rate ) )
122
123 # 3 、 丢 弃 提 升 DART bo oster ( 这 个 论 文 还 要 仔 细 看 )
124 import xgboost as xgb
125 # read in data
126 d tra in = xgb . DMatrix ( ’demo/data/ aga r icu s . txt . t r a i n ’ )
127 d t est = xgb . DMatrix ( ’demo/data/ aga r icu s . txt . t e s t ’ )
128 # s p e c i f y parameters vi a map
129 param = { ’ b ooster ’ : ’ dart ’ ,
130 ’max_depth ’ : 5 , ’ le arnin g_rat e ’ : 0 . 1 ,
131 ’ object i v e ’ : ’ binary : l o g i s t i c ’ , ’ s i l e n t ’ : True ,
132 ’ sample_type ’ : ’ uniform ’ ,
133 ’ normalize_type ’ : ’ t r e e ’ ,
134 ’ rate_drop ’ : 0 . 1 ,
135 ’ skip_drop ’ : 0.5}
136 num_round = 50
137 bst = xgb . t r a i n (param , dtrain , num_round)
138 # make p redict i o n
139 # n tree_ lim it must not be 0
140 preds = bst . p redi c t ( dtest , ntre e_lim it=num_round)
141
XGboost 在 R 下的安装可以参考网址
¯
,下面给出一个简单的示例:
1 #−−−−−−−−−−−−−−−−−−XGboost−example−−−−−−−−−−−−−−−#
¯
http://xgboost.readthedocs.io/en/latest/R-package/xgboostPresentation.html
http://www.ma-xy.com 35 http://www.ma-xy.com