1 选择弱评估器:重要参数booster
for booster in ["gbtree","gblinear","dart"]: reg = XGBR(n_estimators=180 ,learning_rate=0.1 ,random_state=420 ,booster=booster).fit(Xtrain,Ytrain) print(booster) print(reg.score(Xtest,Ytest)) #自己找线性数据试试看"gblinear"的效果吧~
2 XGB的目标函数:重要参数objective
#默认reg:linear reg = XGBR(n_estimators=180,random_state=420).fit(Xtrain,Ytrain) reg.score(Xtest,Ytest) MSE(Ytest,reg.predict(Xtest)) #xgb实现法 import xgboost as xgb #使用类Dmatrix读取数据 dtrain = xgb.DMatrix(Xtrain,Ytrain) dtest = xgb.DMatrix(Xtest,Ytest) #非常遗憾无法打开来查看,所以通常都是先读到pandas里面查看之后再放到DMatrix中 dtrain #写明参数,silent默认为False,通常需要手动将它关闭 param = {'silent':False,'objective':'reg:linear',"eta":0.1} num_round = 180 #类train,可以直接导入的参数是训练数据,树的数量,其他参数都需要通过params来导入 bst = xgb.train(param, dtrain, num_round) #接口predict from sklearn.metrics import r2_score r2_score(Ytest,bst.predict(dtest)) MSE(Ytest,bst.predict(dtest))
3 求解XGB的目标函数