Skip to content

Commit 5cd3e6f

Browse files
movelikerivermengxr
authored andcommitted
[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 <mars.lenjoy@gmail.com> Closes #11125 from movelikeriver/naive_bayes.
1 parent 764ca18 commit 5cd3e6f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

examples/src/main/python/mllib/naive_bayes_example.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717

1818
"""
1919
NaiveBayes Example.
20+
21+
Usage:
22+
`spark-submit --master local[4] examples/src/main/python/mllib/naive_bayes_example.py`
2023
"""
24+
2125
from __future__ import print_function
2226

27+
import shutil
28+
2329
from pyspark import SparkContext
2430
# $example on$
2531
from pyspark.mllib.classification import NaiveBayes, NaiveBayesModel
@@ -50,8 +56,15 @@ def parseLine(line):
5056
# Make prediction and test accuracy.
5157
predictionAndLabel = test.map(lambda p: (model.predict(p.features), p.label))
5258
accuracy = 1.0 * predictionAndLabel.filter(lambda (x, v): x == v).count() / test.count()
59+
print('model accuracy {}'.format(accuracy))
5360

5461
# Save and load model
55-
model.save(sc, "target/tmp/myNaiveBayesModel")
56-
sameModel = NaiveBayesModel.load(sc, "target/tmp/myNaiveBayesModel")
62+
output_dir = 'target/tmp/myNaiveBayesModel'
63+
shutil.rmtree(output_dir, ignore_errors=True)
64+
model.save(sc, output_dir)
65+
sameModel = NaiveBayesModel.load(sc, output_dir)
66+
predictionAndLabel = test.map(lambda p: (sameModel.predict(p.features), p.label))
67+
accuracy = 1.0 * predictionAndLabel.filter(lambda (x, v): x == v).count() / test.count()
68+
print('sameModel accuracy {}'.format(accuracy))
69+
5770
# $example off$

0 commit comments

Comments
 (0)