Back to home page

OSCL-LXR

 
 

    


0001 #
0002 # Licensed to the Apache Software Foundation (ASF) under one or more
0003 # contributor license agreements.  See the NOTICE file distributed with
0004 # this work for additional information regarding copyright ownership.
0005 # The ASF licenses this file to You under the Apache License, Version 2.0
0006 # (the "License"); you may not use this file except in compliance with
0007 # the License.  You may obtain a copy of the License at
0008 #
0009 #    http://www.apache.org/licenses/LICENSE-2.0
0010 #
0011 # Unless required by applicable law or agreed to in writing, software
0012 # distributed under the License is distributed on an "AS IS" BASIS,
0013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014 # See the License for the specific language governing permissions and
0015 # limitations under the License.
0016 #
0017 
0018 """
0019 NaiveBayes Example.
0020 
0021 Usage:
0022   `spark-submit --master local[4] examples/src/main/python/mllib/naive_bayes_example.py`
0023 """
0024 
0025 from __future__ import print_function
0026 
0027 import shutil
0028 
0029 from pyspark import SparkContext
0030 # $example on$
0031 from pyspark.mllib.classification import NaiveBayes, NaiveBayesModel
0032 from pyspark.mllib.util import MLUtils
0033 
0034 
0035 # $example off$
0036 
0037 if __name__ == "__main__":
0038 
0039     sc = SparkContext(appName="PythonNaiveBayesExample")
0040 
0041     # $example on$
0042     # Load and parse the data file.
0043     data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
0044 
0045     # Split data approximately into training (60%) and test (40%)
0046     training, test = data.randomSplit([0.6, 0.4])
0047 
0048     # Train a naive Bayes model.
0049     model = NaiveBayes.train(training, 1.0)
0050 
0051     # Make prediction and test accuracy.
0052     predictionAndLabel = test.map(lambda p: (model.predict(p.features), p.label))
0053     accuracy = 1.0 * predictionAndLabel.filter(lambda pl: pl[0] == pl[1]).count() / test.count()
0054     print('model accuracy {}'.format(accuracy))
0055 
0056     # Save and load model
0057     output_dir = 'target/tmp/myNaiveBayesModel'
0058     shutil.rmtree(output_dir, ignore_errors=True)
0059     model.save(sc, output_dir)
0060     sameModel = NaiveBayesModel.load(sc, output_dir)
0061     predictionAndLabel = test.map(lambda p: (sameModel.predict(p.features), p.label))
0062     accuracy = 1.0 * predictionAndLabel.filter(lambda pl: pl[0] == pl[1]).count() / test.count()
0063     print('sameModel accuracy {}'.format(accuracy))
0064 
0065     # $example off$