Repository: spark
Updated Branches:
  refs/heads/master 764ca1803 -> 5cd3e6f60


[SPARK-13257][IMPROVEMENT] Refine naive Bayes example by checking model after 
loading it

Refine naive Bayes example by checking model after loading it

Author: movelikeriver <[email protected]>

Closes #11125 from movelikeriver/naive_bayes.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/5cd3e6f6
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/5cd3e6f6
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/5cd3e6f6

Branch: refs/heads/master
Commit: 5cd3e6f60b839909210500b319cf312de026dd49
Parents: 764ca18
Author: movelikeriver <[email protected]>
Authored: Mon Feb 22 23:58:54 2016 -0800
Committer: Xiangrui Meng <[email protected]>
Committed: Mon Feb 22 23:58:54 2016 -0800

----------------------------------------------------------------------
 .../src/main/python/mllib/naive_bayes_example.py   | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/5cd3e6f6/examples/src/main/python/mllib/naive_bayes_example.py
----------------------------------------------------------------------
diff --git a/examples/src/main/python/mllib/naive_bayes_example.py 
b/examples/src/main/python/mllib/naive_bayes_example.py
index f5e120c..e7d5893 100644
--- a/examples/src/main/python/mllib/naive_bayes_example.py
+++ b/examples/src/main/python/mllib/naive_bayes_example.py
@@ -17,9 +17,15 @@
 
 """
 NaiveBayes Example.
+
+Usage:
+  `spark-submit --master local[4] 
examples/src/main/python/mllib/naive_bayes_example.py`
 """
+
 from __future__ import print_function
 
+import shutil
+
 from pyspark import SparkContext
 # $example on$
 from pyspark.mllib.classification import NaiveBayes, NaiveBayesModel
@@ -50,8 +56,15 @@ if __name__ == "__main__":
     # Make prediction and test accuracy.
     predictionAndLabel = test.map(lambda p: (model.predict(p.features), 
p.label))
     accuracy = 1.0 * predictionAndLabel.filter(lambda (x, v): x == v).count() 
/ test.count()
+    print('model accuracy {}'.format(accuracy))
 
     # Save and load model
-    model.save(sc, "target/tmp/myNaiveBayesModel")
-    sameModel = NaiveBayesModel.load(sc, "target/tmp/myNaiveBayesModel")
+    output_dir = 'target/tmp/myNaiveBayesModel'
+    shutil.rmtree(output_dir, ignore_errors=True)
+    model.save(sc, output_dir)
+    sameModel = NaiveBayesModel.load(sc, output_dir)
+    predictionAndLabel = test.map(lambda p: (sameModel.predict(p.features), 
p.label))
+    accuracy = 1.0 * predictionAndLabel.filter(lambda (x, v): x == v).count() 
/ test.count()
+    print('sameModel accuracy {}'.format(accuracy))
+
     # $example off$


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to