本文整理汇总了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)