c语言sscanf函数的用法是什么
309
2022-09-02
Spark mllib 决策树
package com.immooc.sparkimport org.apache.log4j.{Level, Logger}import org.apache.spark.mllib.linalg.Vectorsimport org.apache.spark.mllib.regression.LabeledPointimport org.apache.spark.mllib.tree.DecisionTreeimport org.apache.spark.{SparkConf, SparkContext}import org.apache.spark.mllib.util.MLUtilsobject DecisionTreeTest { def main(args:Array[String]): Unit = { val conf = new SparkConf().setAppName("DecisionTreeTest").setMaster("local[2]") val sc = new SparkContext(conf) Logger.getRootLogger.setLevel(Level.WARN) // 读取样本数据1,格式为LIBSVM format val data = sc.textFile("file:///Users/walle/Documents/D3/sparkmlib/data.txt") val parsedData = data.map{ line => val parts = line.split(',') LabeledPoint(parts(0).toDouble, Vectors.dense(parts(1).split(' ').map(_.toDouble))) } //样本数据划分训练样本与测试样本 val splits = parsedData.randomSplit(Array(0.7, 0.3), seed = 11L) val training = splits(0).cache() val test = splits(1) val numClasses = 2 val categoricalFeaturesInfo = Map[Int, Int]() val impurity = "gini" val maxDepth = 5 val maxBins = 32 val model = DecisionTree.trainClassifier(training, numClasses, categoricalFeaturesInfo, impurity, maxDepth, maxBins) //模型预测 val labelAndPreds = test.map { point => val prediction = model.predict(point.features) (point.label, prediction) } //测试值与真实值对比 val print_predict = labelAndPreds.take(15) println("label" + "\t" + "prediction") for (i <- 0 to print_predict.length - 1) { println(print_predict(i)._1 + "\t" + print_predict(i)._2) } //树的错误率 val testErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / test.count() println("Test Error = " + testErr) //打印树的判断值 println("Learned classification tree model:\n" + model.toDebugString) }}
1. 数据
0,32 1 1 0 0,25 1 2 0 1,29 1 2 1 1,24 1 1 0 0,31 1 1 0 1,35 1 2 1 0,30 0 1 0 0,31 1 1 0 1,30 1 2 1 1,21 1 1 0 0,21 1 2 0 1,21 1 2 1 0,29 0 2 1 0,29 1 0 1 0,29 0 2 1 1,30 1 1 0
2. 结果
label prediction 1.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 Test Error = 0.4 Learned classification tree model: DecisionTreeModel classifier of depth 5 with 11 nodes If (feature 0 <= 33.5) If (feature 0 <= 30.5) If (feature 1 <= 0.5) Predict: 0.0 Else (feature 1 > 0.5) If (feature 0 <= 27.0) If (feature 2 <= 1.5) Predict: 1.0 Else (feature 2 > 1.5) Predict: 0.0 Else (feature 0 > 27.0) Predict: 1.0 Else (feature 0 > 30.5) Predict: 0.0 Else (feature 0 > 33.5) Predict: 1.0
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~