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 Streaming Linear Regression Example.
0020 """
0021 from __future__ import print_function
0022 
0023 # $example on$
0024 import sys
0025 # $example off$
0026 
0027 from pyspark import SparkContext
0028 from pyspark.streaming import StreamingContext
0029 # $example on$
0030 from pyspark.mllib.linalg import Vectors
0031 from pyspark.mllib.regression import LabeledPoint
0032 from pyspark.mllib.regression import StreamingLinearRegressionWithSGD
0033 # $example off$
0034 
0035 if __name__ == "__main__":
0036     if len(sys.argv) != 3:
0037         print("Usage: streaming_linear_regression_example.py <trainingDir> <testDir>",
0038               file=sys.stderr)
0039         sys.exit(-1)
0040 
0041     sc = SparkContext(appName="PythonLogisticRegressionWithLBFGSExample")
0042     ssc = StreamingContext(sc, 1)
0043 
0044     # $example on$
0045     def parse(lp):
0046         label = float(lp[lp.find('(') + 1: lp.find(',')])
0047         vec = Vectors.dense(lp[lp.find('[') + 1: lp.find(']')].split(','))
0048         return LabeledPoint(label, vec)
0049 
0050     trainingData = ssc.textFileStream(sys.argv[1]).map(parse).cache()
0051     testData = ssc.textFileStream(sys.argv[2]).map(parse)
0052 
0053     numFeatures = 3
0054     model = StreamingLinearRegressionWithSGD()
0055     model.setInitialWeights([0.0, 0.0, 0.0])
0056 
0057     model.trainOn(trainingData)
0058     print(model.predictOnValues(testData.map(lambda lp: (lp.label, lp.features))))
0059 
0060     ssc.start()
0061     ssc.awaitTermination()
0062     # $example off$