Spark ML包随机森林回归

来源:互联网 发布:操盘手训练软件 编辑:程序博客网 时间:2024/05/17 07:27

官方文档:

trainRegressor(input: RDD[LabeledPoint]categoricalFeaturesInfo: Map[IntInt]numTrees: Int,

featureSubsetStrategy: Stringimpurity: StringmaxDepth: Int,

 maxBins: Intseed: Int = Utils.random.nextInt())


我们需要一个LabeledPoint 格式的 训练数据

LabeledPoint 由两部分组成

val input=LabeledPoint(Label,Feature(Vector型))

Label需要为double型

此处我们需要Import LabledPoint 和Vector2个包:

import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.linalg.{Vectors,Vector}


其他参数:

categoricalFeaturesInfo:一个Map,表示离散特征,格式为[colId,该colId对应特征的维度数]

numTrees:树的数量

featureSubsetStrategy:特征采样方法,选用"auto"代表按1/3采样

impurity:计算特征重要性的指标,此处为回归,选用"variance"

maxDepth:树的最大深度

maxBins:树的最大分裂区间数

seed:随机种子,可不填


val model=RandomForest.trainRegressor(...)

val pre=model.predict("test_feature")


pre为最终回归结果










1 0
原创粉丝点击