導航:首頁 > 編程語言 > ndcgpython

ndcgpython

發布時間:2022-05-19 11:29:13

『壹』 xgboost的python包有多少參數

XGBoost參數

XGBoost的參數可以分為三種類型:通用參數、booster參數以及學習目標參數

『貳』 如何在Python上安裝xgboost

安裝
首先安裝XGBoost的C++版本,然後進入源文件的根目錄下的 wrappers文件夾執行如下腳本安裝Python模塊
python setup.py install

安裝完成後按照如下方式導入XGBoost的Python模塊
import xgboost as xgb

=
數據介面
XGBoost可以載入libsvm格式的文本數據,載入的數據格式可以為Numpy的二維數組和XGBoost的二進制的緩存文件。載入的數據存儲在對象DMatrix中。
載入libsvm格式的數據和二進制的緩存文件時可以使用如下方式
dtrain = xgb.DMatrix('train.svm.txt') dtest = xgb.DMatrix('test.svm.buffer')

載入numpy的數組到DMatrix對象時,可以用如下方式
data = np.random.rand(5,10) # 5 entities, each contains 10 features label = np.random.randint(2, size=5) # binary target dtrain = xgb.DMatrix( data, label=label)

將scipy.sparse格式的數據轉化為 DMatrix格式時,可以使用如下方式
csr = scipy.sparse.csr_matrix( (dat, (row,col)) ) dtrain = xgb.DMatrix( csr )

將 DMatrix 格式的數據保存成XGBoost的二進制格式,在下次載入時可以提高載入速度,使用方式如下
dtrain = xgb.DMatrix('train.svm.txt') dtrain.save_binary("train.buffer")

可以用如下方式處理 DMatrix中的缺失值:
dtrain = xgb.DMatrix( data, label=label, missing = -999.0)

當需要給樣本設置權重時,可以用如下方式
w = np.random.rand(5,1) dtrain = xgb.DMatrix( data, label=label, missing = -999.0, weight=w)

參數設置
XGBoost使用key-value格式保存參數. Eg
* Booster(基本學習器)參數
param = {'bst:max_depth':2, 'bst:eta':1, 'silent':1, 'objective':'binary:logistic' } param['nthread'] = 4 plst = param.items() plst += [('eval_metric', 'auc')] # Multiple evals can be handled in this way plst += [('eval_metric', 'ams@0')]

還可以定義驗證數據集,驗證演算法的性能
evallist = [(dtest,'eval'), (dtrain,'train')]

=
訓練模型
有了參數列表和數據就可以訓練模型了
* 訓練
num_round = 10 bst = xgb.train( plst, dtrain, num_round, evallist )

保存模型
在訓練完成之後可以將模型保存下來,也可以查看模型內部的結構
bst.save_model('0001.model')

Dump Model and Feature Map
You can mp the model to txt and review the meaning of model
# mp model bst.mp_model('mp.raw.txt') # mp model with feature map bst.mp_model('mp.raw.txt','featmap.txt')

載入模型
通過如下方式可以載入模型
bst = xgb.Booster({'nthread':4}) #init model bst.load_model("model.bin") # load data

=
提前終止程序
如果有評價數據,可以提前終止程序,這樣可以找到最優的迭代次數。如果要提前終止程序必須至少有一個評價數據在參數evals中。 If there』s more than one, it will use the last.
train(..., evals=evals, early_stopping_rounds=10)
The model will train until the validation score stops improving. Validation error needs to decrease at least every early_stopping_rounds to continue training.
If early stopping occurs, the model will have two additional fields: bst.best_score and bst.best_iteration. Note that train() will return a model from the last iteration, not the best one.
This works with both metrics to minimize (RMSE, log loss, etc.) and to maximize (MAP, NDCG, AUC).
=
Prediction
After you training/loading a model and preparing the data, you can start to do prediction.
data = np.random.rand(7,10) # 7 entities, each contains 10 features dtest = xgb.DMatrix( data, missing = -999.0 ) ypred = bst.predict( xgmat )

If early stopping is enabled ring training, you can predict with the best iteration.
ypred = bst.predict(xgmat,ntree_limit=bst.best_iteration)

閱讀全文

與ndcgpython相關的資料

熱點內容
股票漲跌源碼怎麼看 瀏覽:575
加密軟體做法 瀏覽:54
美國程序員有多少中國人 瀏覽:739
人民日報app里怎麼看新聞早班車 瀏覽:584
忘了app怎麼辦 瀏覽:528
如何用雲伺服器做雲平台 瀏覽:298
非箍筋加密區剪力 瀏覽:116
利聯科技伺服器卡怎麼辦 瀏覽:383
js和python哪個好 瀏覽:460
c盤的哪些文件夾沒用 瀏覽:80
文件為什麼會超出在線解壓限制 瀏覽:588
python類實例化對象 瀏覽:794
硬碟dos外部命令 瀏覽:792
做演算法還是開發 瀏覽:872
按鍵精靈自定義圖層命令 瀏覽:353
魅藍3手機音視頻文件夾 瀏覽:945
安卓手機製表怎麼換行 瀏覽:215
牆柱搭接箍筋怎麼加密 瀏覽:456
怎麼加密不讓人打開 瀏覽:336
2g3g演算法 瀏覽:206