/
SparseNaiveBayes.scala
104 lines (85 loc) · 3.53 KB
/
SparseNaiveBayes.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// scalastyle:off println
package org.apache.spark.examples.mllib
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.core.config.Configurator
import scopt.OptionParser
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.classification.NaiveBayes
import org.apache.spark.mllib.util.MLUtils
/**
* An example naive Bayes app. Run with
* {{{
* ./bin/run-example org.apache.spark.examples.mllib.SparseNaiveBayes [options] <input>
* }}}
* If you use it as a template to create your own app, please use `spark-submit` to submit your app.
*/
object SparseNaiveBayes {
case class Params(
input: String = null,
minPartitions: Int = 0,
numFeatures: Int = -1,
lambda: Double = 1.0) extends AbstractParams[Params]
def main(args: Array[String]): Unit = {
val defaultParams = Params()
val parser = new OptionParser[Params]("SparseNaiveBayes") {
head("SparseNaiveBayes: an example naive Bayes app for LIBSVM data.")
opt[Int]("numPartitions")
.text("min number of partitions")
.action((x, c) => c.copy(minPartitions = x))
opt[Int]("numFeatures")
.text("number of features")
.action((x, c) => c.copy(numFeatures = x))
opt[Double]("lambda")
.text(s"lambda (smoothing constant), default: ${defaultParams.lambda}")
.action((x, c) => c.copy(lambda = x))
arg[String]("<input>")
.text("input paths to labeled examples in LIBSVM format")
.required()
.action((x, c) => c.copy(input = x))
}
parser.parse(args, defaultParams) match {
case Some(params) => run(params)
case _ => sys.exit(1)
}
}
def run(params: Params): Unit = {
val conf = new SparkConf().setAppName(s"SparseNaiveBayes with $params")
val sc = new SparkContext(conf)
Configurator.setRootLevel(Level.WARN)
val minPartitions =
if (params.minPartitions > 0) params.minPartitions else sc.defaultMinPartitions
val examples =
MLUtils.loadLibSVMFile(sc, params.input, params.numFeatures, minPartitions)
// Cache examples because it will be used in both training and evaluation.
examples.cache()
val splits = examples.randomSplit(Array(0.8, 0.2))
val training = splits(0)
val test = splits(1)
val numTraining = training.count()
val numTest = test.count()
println(s"numTraining = $numTraining, numTest = $numTest.")
val model = new NaiveBayes().setLambda(params.lambda).run(training)
val prediction = model.predict(test.map(_.features))
val predictionAndLabel = prediction.zip(test.map(_.label))
val accuracy = predictionAndLabel.filter(x => x._1 == x._2).count().toDouble / numTest
println(s"Test accuracy = $accuracy.")
sc.stop()
}
}
// scalastyle:on println