xgboost的SparkWithDataFrame版本实现
再xgboost的源码中有xgboost的SparkWithDataFrame的实现,如下:https://github.com/dmlc/xgboost/tree/master/jvm-packages。但是由于各种各样的原因吧,这些代码在我的IDE里面编译不过,因此又写了如下代码以供以后查阅使用。
package xgboost
import ml.dmlc.xgboost4j.scala.spark.{XGBoost, XGBoostModel}
import org.apache.spark.mllib.evaluation.BinaryClassificationMetrics
import org.apache.spark.sql.{Row, DataFrame, SparkSession}
object App{
def main(args: Array[String]): Unit ={
val trainPath: String = "xxx/train.txt"
val testPath: String = "xxx/test.txt"
val binaryModelPath: String = "xxx/model.binary"
val textModelPath: String = "xxx/model.txt"
val spark = SparkSession
.builder()
.master("yarn")
.getOrCreate()
// define xgboost parameters
val maxDepth = 3
val numRound = 4
val nworker = 1
val paramMap = List(
"eta" -> 0.1,
"max_depth" -> maxDepth,
"objective" -> "binary:logistic").toMap
//read libsvm file
var dfTrain = spark.read.format("libsvm").load(trainPath).toDF("labelCol", "featureCol")
var dfTest = spark.read.format("libsvm").load(testPath).toDF("labelCol", "featureCol")
dfTrain.show(true)
printf("begin...")
val model:XGBoostModel = XGBoost.trainWithDataFrame(dfTrain, paramMap, numRound, nworker,
useExternalMemory = true,
featureCol = "featureCol", labelCol = "labelCol",
missing = 0.0f)
//predict the test set
val predict:DataFrame = model.transform(dfTest)
val scoreAndLabels = predict.select(model.getPredictionCol, model.getLabelCol)
.rdd
.map{case Row(score:Double, label:Double) => (score, label)}
//get the auc
val metric = new BinaryClassificationMetrics(scoreAndLabels)
val auc = metric.areaUnderROC()
println("auc:" + auc)
//save model
this.saveBinaryModel(model, spark, binaryModelPath)
this.saveTextModel(model, spark, textModelPath, numRound, maxDepth)
}
def saveBinaryModel(model:XGBoostModel, spark: SparkSession, path: String): Unit = {
model.saveModelAsHadoopFile(path)(spark.sparkContext)
}
def saveTextModel(model:XGBoostModel, spark: SparkSession, path: String, numRound: Int, maxDepth: Int): Unit = {
val dumpModel = model
.booster
.getModelDump()
.toList
.zipWithIndex
.map(x => s"booster:[${x._2}]\n${x._1}")
val header = s"numRound: $numRound, maxDepth: $maxDepth"
print(dumpModel)
import spark.implicits._
val text: List[String] = header +: dumpModel
text.toDF
.coalesce(1)
.write
.mode("overwrite")
.text(path)
}
}
其中:
1.训练集和测试集都是libsvm格式,如下所示:
1 3:1 10:1 11:1 21:1 30:1 34:1 36:1 40:1 41:1 53:1 58:1 65:1 69:1 77:1 86:1 88:1 92:1 95:1 102:1 105:1 117:1 124:1
0 3:1 10:1 20:1 21:1 23:1 34:1 36:1 39:1 41:1 53:1 56:1 65:1 69:1 77:1 86:1 88:1 92:1 95:1 102:1 106:1 116:1 120:1
2.最终生成的模型如下所示:
numRound: 4, maxDepth: 3
booster:[0]
0:[f29<] yes=1,no=2,missing=2
1:leaf=0.152941
2:leaf=-0.191209 booster:[1]
0:[f29<2] yes=1,no=2,missing=2
1:leaf=0.141901
2:leaf=-0.174499 booster:[2]
0:[f29<2] yes=1,no=2,missing=2
1:leaf=0.132731
2:leaf=-0.161685 booster:[3]
0:[f29<2] yes=1,no=2,missing=2
1:leaf=0.124972
2:leaf=-0.15155
相关解释:”numRound: 4, maxDepth: 3”表示生成树的个数为4,树的最大深度为3;booster[n]表示第n棵树;以下保存树的结构,0号节点为根节点,每个节点有两个子节点,节点序号按层序技术,即1号和2号节点为根节点0号节点的子节点,相同层的节点有相同缩进,且比父节点多一级缩进。
在节点行,首先声明节点序号,中括号里写明该节点采用第几个特征(如f29即为训练数据的第29个特征),同时表明特征值划分条件,“[f29<2] yes=1,no=2,missing=2”:表示f29号特征大于2时该样本划分到1号叶子节点,f29>=2时划分到2号叶子节点,当没有该特征(None)划分到2号叶子节点。
3.预测的结果如下:
|labelCol|featureCol |probabilities |prediction|
|1.0 |(126,[2,9,10,20,29,33,35,39,40,52,57,64,68,76,85,87,91,94,101,104,116,123],[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0])|[0.3652743101119995,0.6347256898880005]|1.0 |
|0.0 |(126,[2,9,19,20,22,33,35,38,40,52,55,64,68,76,85,87,91,94,101,105,115,119],[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0])|[0.6635029911994934,0.3364970088005066]|0.0 |
xgboost的SparkWithDataFrame版本实现的更多相关文章
- 在Window平台下安装xgboost的Python版本
原文:http://blog.csdn.net/pengyulong/article/details/50515916 原文修改了两个地方才安装成功,第3步可以不用,第2步重新生成所有的就行了. 第4 ...
- 小巧玲珑:机器学习届快刀XGBoost的介绍和使用
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:张萌 序言 XGBoost效率很高,在Kaggle等诸多比赛中使用广泛,并且取得了不少好成绩.为了让公司的算法工程师,可以更加方便的 ...
- xgboost 参数调优指南
一.XGBoost的优势 XGBoost算法可以给预测模型带来能力的提升.当我对它的表现有更多了解的时候,当我对它的高准确率背后的原理有更多了解的时候,我发现它具有很多优势: 1 正则化 标准GBDT ...
- XGBoost 与 Boosted Tree
http://www.52cs.org/?p=429 作者:陈天奇,毕业于上海交通大学ACM班,现就读于华盛顿大学,从事大规模机器学习研究. 注解:truth4sex 编者按:本文是对开源xgboo ...
- xgboost入门与实战(原理篇)
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- 机器学习--boosting家族之XGBoost算法
一.概念 XGBoost全名叫(eXtreme Gradient Boosting)极端梯度提升,经常被用在一些比赛中,其效果显著.它是大规模并行boosted tree的工具,它是目前最快最好的开源 ...
- xgboost 参数
XGBoost 参数 在运行XGBoost程序之前,必须设置三种类型的参数:通用类型参数(general parameters).booster参数和学习任务参数(task parameters). ...
- XGBoost:在Python中使用XGBoost
原文:http://blog.csdn.net/zc02051126/article/details/46771793 在Python中使用XGBoost 下面将介绍XGBoost的Python模块, ...
- 【转】XGBoost 与 Boosted Tree
XGBoost 与 Boosted Tree http://www.52cs.org/?p=429 作者:陈天奇,毕业于上海交通大学ACM班,现就读于华盛顿大学,从事大规模机器学习研究. 注解:tru ...
随机推荐
- 编译TensorFlow CPU指令集优化版
编译TensorFlow CPU指令集优化版 如题,CPU指令集优化版,说的是针对某种特定的CPU型号进行过优化的版本.通常官方给的版本是没有针对特定CPU进行过优化的,有网友称,优化过的版本相比优化 ...
- 牛客网数据库SQL实战
查找最晚入职员工的所有信息 CREATE TABLE `employees` (`emp_no` int(11) NOT NULL,`birth_date` date NOT NULL,`first_ ...
- ICPC南京补题
由于缺的题目比较多,竟然高达3题,所以再写一篇补题的博客 Lpl and Energy-saving Lamps During tea-drinking, princess, amongst othe ...
- 九度oj 题目1416:猴子吃坚果
题目描述: 动物园的猴子吃坚果的顺序都是按强壮程度来定的,最强壮的吃完才能轮到下一个,现在我们给出各个猴子的名字,强壮程度,吃饱的量,然后查询对应的猴子必须要扔多少坚果才可以轮到. 输入: 输入有多组 ...
- Android强制更新
代码改变世界 Android版本强制更新 package com.lianpos.util; import android.content.Context; import android.conten ...
- 【bzoj2111】[ZJOI2010]Perm 排列计数 dp+Lucas定理
题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Mogic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Mogic的,答案可能很 ...
- Educational Codeforces Round 10——B. z-sort
B. z-sort time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- 算法复习——哈希表+折半搜索(poj2549)
搬讲义~搬讲义~ 折半搜索感觉每次都是打暴力时用的啊2333,主要是用于降次··当复杂度为指数级别时用折半可以减少大量复杂度··其实专门考折半的例题并不多···一般都是中途的一个小优化··· 然后折半 ...
- [BZOJ4260] Codechef REBXOR (01字典树,异或前缀和)
Description Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,-,AN. Output 输出一行包含给定表达式可能的最大值. Sample ...
- docker 镜像阿里加速器
1.登录 https://cr.console.aliyun.com/#/imageList 2.点击加速器tab,获取自己的加速器地址,然后执行黑框内的命令. .sudo mkdir -p /etc ...