Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
* limitations under the License.
*/

/*
* Usage:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this usage text probably isn't the format we want, its pretty specific to each Spark release. Maybe look at some of the other files in this directory and see how they do their usage?

*
* sbt package
*
* spark-submit --class "org.apache.spark.examples.mllib.NaiveBayesExample"
* --master local[4]
* examples/target/scala-2.11/spark-examples_2.11-2.0.0-SNAPSHOT.jar
*/

// scalastyle:off println
package org.apache.spark.examples.mllib

Expand All @@ -23,6 +33,9 @@ import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.mllib.classification.{NaiveBayes, NaiveBayesModel}
import org.apache.spark.mllib.linalg.Vectors
import org.apache.spark.mllib.regression.LabeledPoint

import java.io.File;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports don't seem to be ordered in the way we would want them to be. You should check out https://cwiki.apache.org/confluence/display/SPARK/Contributing+to+Spark and https://cwiki.apache.org/confluence/display/SPARK/Spark+Code+Style+Guide

import org.apache.commons.io.FileUtils;
// $example off$

object NaiveBayesExample {
Expand All @@ -46,10 +59,18 @@ object NaiveBayesExample {

val predictionAndLabel = test.map(p => (model.predict(p.features), p.label))
val accuracy = 1.0 * predictionAndLabel.filter(x => x._1 == x._2).count() / test.count()
println("model accuracy %f".format(accuracy))

// Save and load model
model.save(sc, "target/tmp/myNaiveBayesModel")
val sameModel = NaiveBayesModel.load(sc, "target/tmp/myNaiveBayesModel")
val outputDir = "target/tmp/myNaiveBayesModel"
FileUtils.forceDelete(new File(outputDir))
model.save(sc, outputDir)
val sameModel = NaiveBayesModel.load(sc, outputDir)

val samePredictionAndLabel = test.map(p => (sameModel.predict(p.features), p.label))
val sameAccuracy = 1.0 * samePredictionAndLabel.filter(x => x._1 == x._2).count() /
test.count()
println("sameModel accuracy %f".format(sameAccuracy))
// $example off$
}
}
Expand Down