今天学习写scala,拿xgboost试一下~
先记一下xgboost调参要点:
7.xgboost中比较重要的参数介绍
(1)objective [ default=reg:linear ] 定义学习任务及相应的学习目标,可选的目标函数如下:
“reg:linear” –线性回归。
“reg:logistic” –逻辑回归。
“binary:logistic” –二分类的逻辑回归问题,输出为概率。
“binary:logitraw” –二分类的逻辑回归问题,输出的结果为wTx。
“count:poisson” –计数问题的poisson回归,输出结果为poisson分布。 在poisson回归中,max_delta_step的缺省值为0.7。(used to safeguard optimization)
“multi:softmax” –让XGBoost采用softmax目标函数处理多分类问题,同时需要设置参数num_class(类别个数)
“multi:softprob” –和softmax一样,但是输出的是ndata * nclass的向量,可以将该向量reshape成ndata行nclass列的矩阵。没行数据表示样本所属于每个类别的概率。
“rank:pairwise” –set XGBoost to do ranking task by minimizing the pairwise loss
(2)’eval_metric’ The choices are listed below,评估指标:
“rmse”: root mean square error
“logloss”: negative log-likelihood
“error”: Binary classification error rate. It is calculated as #(wrong cases)/#(all cases). For the predictions, the evaluation will regard the instances with prediction value larger than 0.5 as positive instances, and the others as negative instances.
“merror”: Multiclass classification error rate. It is calculated as #(wrong cases)/#(all cases).
“mlogloss”: Multiclass logloss
“auc”: Area under the curve for ranking evaluation.
“ndcg”:Normalized Discounted Cumulative Gain
“map”:Mean average precision
“ndcg@n”,”map@n”: n can be assigned as an integer to cut off the top positions in the lists for evaluation.
“ndcg-“,”map-“,”ndcg@n-“,”map@n-“: In XGBoost, NDCG and MAP will evaluate the score of a list without any positive samples as 1. By adding “-” in the evaluation metric XGBoost will evaluate these score as 0 to be consistent under some conditions.
(3)lambda [default=0] L2 正则的惩罚系数
(4)alpha [default=0] L1 正则的惩罚系数
(5)lambda_bias 在偏置上的L2正则。缺省值为0(在L1上没有偏置项的正则,因为L1时偏置不重要)
(6)eta [default=0.3]
为了防止过拟合,更新过程中用到的收缩步长。在每次提升计算之后,算法会直接获得新特征的权重。 eta通过缩减特征的权重使提升计算过程更加保守。缺省值为0.3
取值范围为:[0,1]
(7)max_depth [default=6] 数的最大深度。缺省值为6 ,取值范围为:[1,∞]
(8)min_child_weight [default=1]
孩子节点中最小的样本权重和。如果一个叶子节点的样本权重和小于min_child_weight则拆分过程结束。在现行回归模型中,这个参数是指建立每个模型所需要的最小样本数。该成熟越大算法越conservative
取值范围为: [0,∞]
scala代码:
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.tree.RandomForest
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}
import ml.dmlc.xgboost4j.scala.spark.XGBoost
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint
val df=sqlContext.sql("select * from xxxx where xxxx)
val data=df.select(
df("userid"),
df("f1"),
df("f2"),
df("l1"))
val trainData=data.map{line =>
val label=line(3).toString.toDouble
val value0=(0 to 2).map(i=> line(i).toString.toDouble )
val featureVector=Vectors.dense( value0.toArray)
LabeledPoint(label, featureVector)
}
val numRound = 800
val paramMap = List(
"eta" -> 0.1f,
"max_depth" -> 6, -- 数的最大深度。缺省值为6 ,取值范围为:[1,∞]
"silent" -> 0, -- 取0时表示打印出运行时信息,取1时表示以缄默方式运行,不打印运行时信息。缺省值为0
"objective" -> "reg:linear", -- 定义学习任务及相应的学习目标
"eval_metric" -> "rmse", -- 校验数据所需要的评价指标
"nthread"-> 1 -- XGBoost运行时的线程数。缺省值是当前系统可以获得的最大线程数
).toMap
val model = XGBoost.train(trainData, paramMap, numRound, nWorkers = 80, useExternalMemory = false)
本文介绍了作者使用Scala结合Spark进行XGBoost模型调参的经验,包括关键步骤和代码示例。
4003

被折叠的 条评论
为什么被折叠?



