请选择 进入手机版 | 继续访问电脑版
楼主: ReneeBK
1295 1

[Scala Study]Decision Tree Regression Using Scala [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49402 个
通用积分
51.7504
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

ReneeBK 发表于 2015-11-16 00:30:16 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. // scalastyle:off println
  2. package org.apache.spark.examples.ml
  3. import org.apache.spark.sql.SQLContext
  4. import org.apache.spark.{SparkContext, SparkConf}
  5. // $example on$
  6. import org.apache.spark.ml.Pipeline
  7. import org.apache.spark.ml.regression.DecisionTreeRegressor
  8. import org.apache.spark.ml.regression.DecisionTreeRegressionModel
  9. import org.apache.spark.ml.feature.VectorIndexer
  10. import org.apache.spark.ml.evaluation.RegressionEvaluator
  11. // $example off$
  12. object DecisionTreeRegressionExample {
  13.   def main(args: Array[String]): Unit = {
  14.     val conf = new SparkConf().setAppName("DecisionTreeRegressionExample")
  15.     val sc = new SparkContext(conf)
  16.     val sqlContext = new SQLContext(sc)

  17.     // $example on$
  18.     // Load the data stored in LIBSVM format as a DataFrame.
  19.     val data = sqlContext.read.format("libsvm").load("data/mllib/sample_libsvm_data.txt")

  20.     // Automatically identify categorical features, and index them.
  21.     // Here, we treat features with > 4 distinct values as continuous.
  22.     val featureIndexer = new VectorIndexer()
  23.       .setInputCol("features")
  24.       .setOutputCol("indexedFeatures")
  25.       .setMaxCategories(4)
  26.       .fit(data)

  27.     // Split the data into training and test sets (30% held out for testing)
  28.     val Array(trainingData, testData) = data.randomSplit(Array(0.7, 0.3))

  29.     // Train a DecisionTree model.
  30.     val dt = new DecisionTreeRegressor()
  31.       .setLabelCol("label")
  32.       .setFeaturesCol("indexedFeatures")

  33.     // Chain indexer and tree in a Pipeline
  34.     val pipeline = new Pipeline()
  35.       .setStages(Array(featureIndexer, dt))

  36.     // Train model.  This also runs the indexer.
  37.     val model = pipeline.fit(trainingData)

  38.     // Make predictions.
  39.     val predictions = model.transform(testData)

  40.     // Select example rows to display.
  41.     predictions.select("prediction", "label", "features").show(5)

  42.     // Select (prediction, true label) and compute test error
  43.     val evaluator = new RegressionEvaluator()
  44.       .setLabelCol("label")
  45.       .setPredictionCol("prediction")
  46.       .setMetricName("rmse")
  47.     val rmse = evaluator.evaluate(predictions)
  48.     println("Root Mean Squared Error (RMSE) on test data = " + rmse)

  49.     val treeModel = model.stages(1).asInstanceOf[DecisionTreeRegressionModel]
  50.     println("Learned regression tree model:\n" + treeModel.toDebugString)
  51.     // $example off$
  52.   }
  53. }
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:regression regressio Decision regress SCALA example import

本帖被以下文库推荐

ccww 发表于 2016-4-14 01:26:16 来自手机 |显示全部楼层 |坛友微信交流群
Scala for Data Science

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 07:10