Xgboost的简单使用2
来源:互联网 发布:客管家软件好用吗 编辑:程序博客网 时间:2024/05/21 14:43
1、xgboost预测
#xgboost预测import xgboost as xgb# read in datadtrain = xgb.DMatrix('demo/data/agaricus.txt.train')dtest = xgb.DMatrix('demo/data/agaricus.txt.test')# specify parameters via mapparam = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic' }num_round = 2bst = xgb.train(param, dtrain, num_round)# make predictionpreds = bst.predict(dtest)
知识点:
1bst = xgb.train(param, dtrain, num_round)
2.
#!/usr/bin/pythonimport numpy as npimport scipy.sparseimport pickleimport xgboost as xgb### simple example# load file from text file, also binary buffer generated by xgboostdtrain = xgb.DMatrix('agaricus.txt.train')dtest = xgb.DMatrix('agaricus.txt.test')# specify parameters via map, definition are same as c++ versionparam = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'}# specify validations set to watch performancewatchlist = [(dtest, 'eval'), (dtrain, 'train')]num_round = 2bst = xgb.train(param, dtrain, num_round, watchlist)# this is predictionpreds = bst.predict(dtest)labels = dtest.get_label()print('error=%f' % (sum(1 for i in range(len(preds)) if int(preds[i] > 0.5) != labels[i]) / float(len(preds))))bst.save_model('0001.model')# dump modelbst.dump_model('dump.raw.txt')# dump model with feature mapbst.dump_model('dump.nice.txt', 'featmap.txt')# save dmatrix into binary bufferdtest.save_binary('dtest.buffer')# save modelbst.save_model('xgb.model')# load model and data inbst2 = xgb.Booster(model_file='xgb.model')dtest2 = xgb.DMatrix('dtest.buffer')preds2 = bst2.predict(dtest2)# assert they are the sameassert np.sum(np.abs(preds2 - preds)) == 0# alternatively, you can pickle the boosterpks = pickle.dumps(bst2)# load model and data inbst3 = pickle.loads(pks)preds3 = bst3.predict(dtest2)# assert they are the sameassert np.sum(np.abs(preds3 - preds)) == 0#### build dmatrix from scipy.sparseprint('start running example of build DMatrix from scipy.sparse CSR Matrix')labels = []row = []; col = []; dat = []i = 0for l in open('agaricus.txt.train'): arr = l.split() labels.append(int(arr[0])) for it in arr[1:]: k,v = it.split(':') row.append(i); col.append(int(k)); dat.append(float(v)) i += 1csr = scipy.sparse.csr_matrix((dat, (row, col)))dtrain = xgb.DMatrix(csr, label=labels)watchlist = [(dtest, 'eval'), (dtrain, 'train')]bst = xgb.train(param, dtrain, num_round, watchlist)print('start running example of build DMatrix from scipy.sparse CSC Matrix')# we can also construct from csc matrixcsc = scipy.sparse.csc_matrix((dat, (row, col)))dtrain = xgb.DMatrix(csc, label=labels)watchlist = [(dtest, 'eval'), (dtrain, 'train')]bst = xgb.train(param, dtrain, num_round, watchlist)print('start running example of build DMatrix from numpy array')# NOTE: npymat is numpy array, we will convert it into scipy.sparse.csr_matrix in internal implementation# then convert to DMatrixnpymat = csr.todense()dtrain = xgb.DMatrix(npymat, label=labels)watchlist = [(dtest, 'eval'), (dtrain, 'train')]bst = xgb.train(param, dtrain, num_round, watchlist)
参考:
- 官方demo
阅读全文
0 0
- Xgboost的简单使用2
- xgboost+python参数介绍的简单使用
- python包xgboost安装和简单使用
- xgboost的使用简析
- xgboost使用
- XGBoost Windows 下的 安装 使用
- xgboost使用自定义的loss function
- windows下安装xgboost for python 的简单方法
- XGBoost:在Python中使用XGBoost
- XGBoost:在Python中使用XGBoost
- XGBoost:在Python中使用XGBoost
- xgboost使用调参
- xgboost使用案例一
- xgboost使用案例二
- xgboost使用小结
- xgboost 包使用
- xgboost使用步骤
- xgboost使用案例一
- finereport破解版有吗
- CodeForces 66 D.Petya and His Friends(构造+数论+高精度)
- Visual Builder-低代码开发平台中的AK47
- 计算机视觉之OpenCV教程 --- Mat图像类基础(二)
- 课程26 项目6
- Xgboost的简单使用2
- 40、50、60--说说我那些超龄的程序员同事们
- 拿好不谢!程序员圣诞节的脱单秘籍
- redis事务处理
- 走近富兰克林--《富兰克林自传》
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 G-取数游戏(区间dp)
- 剑指Offer---正则表达式匹配
- C语言中的typedef struct用法
- 最高 3.9 GHz CPU 时脉、Vega M 图形核心 Intel Core i7-8709G 处理器资料曝光