当前位置: 首页>>代码示例>>Scala>>正文


Scala Entropy类代码示例

本文整理汇总了Scala中org.apache.spark.mllib.tree.impurity.Entropy的典型用法代码示例。如果您正苦于以下问题:Scala Entropy类的具体用法?Scala Entropy怎么用?Scala Entropy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Entropy类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1:

//设置package包名称以及导入依赖的类
import org.apache.spark.SparkContext
import org.apache.spark.mllib.tree.DecisionTree
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.tree.configuration.Algo._
import org.apache.spark.mllib.tree.impurity.Entropy

// Load and parse the data file
val tData = sc.textFile("hdfs://129.207.46.225:8020/mlclass/mushroom.training.csv")
val trainningData = tData.filter(line => !line.contains("%")).map { line =>
  var parts = line.split(',').map(str=>{ val cs = str.toCharArray; val c = cs(0); (c-'a').toDouble})
  if(parts(0)<10) parts(0) = 1.0;
  else parts(0) = 0.0;
  LabeledPoint(parts(0), Vectors.dense(parts.tail))
}

val pData = sc.textFile("hdfs://129.207.46.225:8020/mlclass/mushroom.test.csv")
val predictData = pData.filter(line => !line.contains("%")).map { line =>
  var parts = line.split(',').map(str=>{ val cs = str.toCharArray; val c = cs(0); (c-'a').toDouble})
  if(parts(0)<10) parts(0) = 1.0;
  else parts(0) = 0.0;
  LabeledPoint(parts(0), Vectors.dense(parts.tail))
}

// Run training algorithm to build the model
val maxDepth = 5
val model = DecisionTree.train(trainningData, Classification, Entropy, maxDepth)

val labelAndPreds = predictData.map { point =>
  val prediction = model.predict(point.features)
  (point.label, prediction)
}

val trainErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / predictData.count
println("Training Error = " + trainErr) 
开发者ID:hero1122,项目名称:sparkTest,代码行数:36,代码来源:SparkDecisionTreeDiv.scala


注:本文中的org.apache.spark.mllib.tree.impurity.Entropy类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。