导航:首页 > 编程语言 > 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相关的资料

热点内容
python字符串替换函数 浏览:825
app会员卡怎么做 浏览:921
我爱你python编码 浏览:365
一台计算机作为服务器一般可以运行什么软件 浏览:421
应用加密桌面还显示头像咋办 浏览:523
微软硬盘加密密钥 浏览:156
空调压缩机和风扇 浏览:511
代码可以编译运行 浏览:918
银行卡加密码大全真号 浏览:447
单片机测esr 浏览:412
app怎么设置消息功能 浏览:916
明词pdf 浏览:427
云域控服务器有什么用 浏览:577
字节和B站程序员 浏览:747
app推荐书要怎么做 浏览:303
unix网络编程第一卷 浏览:851
c需要pdf 浏览:865
超级解压的美甲 浏览:72
安卓手机如何永久取消静音 浏览:722
appstore免密码支付怎么关闭 浏览:30