本文整理汇总了Scala中org.apache.spark.ml.regression.RandomForestRegressor类的典型用法代码示例。如果您正苦于以下问题:Scala RandomForestRegressor类的具体用法?Scala RandomForestRegressor怎么用?Scala RandomForestRegressor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RandomForestRegressor类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: SitelinkEntry
//设置package包名称以及导入依赖的类
package org.wikimedia.research.recommendation.job.translation
import org.apache.spark.ml.evaluation.RegressionEvaluator
import org.apache.spark.ml.linalg.DenseVector
import org.apache.spark.ml.regression.RandomForestRegressor
import org.apache.spark.sql.{DataFrame, SparkSession}
case class SitelinkEntry(id: String, site: String, title: String)
case class PagecountEntry(site: String, title: String, pageviews: Double)
case class SitelinkPageviewsEntry(id: String, site: String, title: String, pageviews: Double)
case class RankedEntry(id: String, site: String, title: String, pageviews: Double, rank: Double)
object Utils {
val FEATURES = "features"
val LABEL = "label"
val PREDICTION = "prediction"
val EXISTS = 1.0
val NOT_EXISTS = 0.0
val REGRESSOR: RandomForestRegressor = new RandomForestRegressor()
.setLabelCol(LABEL)
.setFeaturesCol(FEATURES)
val EVALUATOR: RegressionEvaluator = new RegressionEvaluator()
.setLabelCol(LABEL)
.setPredictionCol(PREDICTION)
.setMetricName("rmse")
def getWorkData(spark: SparkSession, data: DataFrame, target: String, exists: Boolean = true): DataFrame = {
val workData: DataFrame = data.filter(row =>
row(row.fieldIndex("exists_" + target)) == (if (exists) EXISTS else NOT_EXISTS))
import spark.implicits._
val labeledData = workData.map(row =>
(
row.getString(row.fieldIndex("id")),
row.getDouble(row.fieldIndex("rank_" + target)),
new DenseVector((
(1 until row.fieldIndex("pageviews_" + target)).map(row.getDouble) ++
(row.fieldIndex("exists_" + target) + 1 until row.length).map(row.getDouble)
).toArray)
)
).rdd
spark.createDataFrame(labeledData).toDF("id", LABEL, FEATURES)
}
}