org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD Scala Examples
The following examples show how to use org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: StreamingLinearRegressionExample.scala From drizzle-spark with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.mllib import org.apache.spark.SparkConf // $example on$ import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD // $example off$ import org.apache.spark.streaming._ object StreamingLinearRegressionExample { def main(args: Array[String]): Unit = { if (args.length != 2) { System.err.println("Usage: StreamingLinearRegressionExample <trainingDir> <testDir>") System.exit(1) } val conf = new SparkConf().setAppName("StreamingLinearRegressionExample") val ssc = new StreamingContext(conf, Seconds(1)) // $example on$ val trainingData = ssc.textFileStream(args(0)).map(LabeledPoint.parse).cache() val testData = ssc.textFileStream(args(1)).map(LabeledPoint.parse) val numFeatures = 3 val model = new StreamingLinearRegressionWithSGD() .setInitialWeights(Vectors.zeros(numFeatures)) model.trainOn(trainingData) model.predictOnValues(testData.map(lp => (lp.label, lp.features))).print() ssc.start() ssc.awaitTermination() // $example off$ ssc.stop() } } // scalastyle:on println
Example 2
Source File: StreamingSimpleModel.scala From AI with Apache License 2.0 | 5 votes |
package com.bigchange.streaming import breeze.linalg.DenseVector import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LabeledPoint, StreamingLinearRegressionWithSGD} import org.apache.spark.streaming.{Seconds, StreamingContext} object StreamingSimpleModel { def main(args: Array[String]) { val ssc = new StreamingContext("local","test",Seconds(10)) val stream = ssc.socketTextStream("localhost",9999) val numberFeatures = 100 val zeroVector = DenseVector.zeros[Double](numberFeatures) val model = new StreamingLinearRegressionWithSGD() .setInitialWeights(Vectors.dense(zeroVector.data)) .setNumIterations(1) .setStepSize(0.01) val labeledStream = stream.map { event => val split = event.split("\t") val y = split(0).toDouble val features = split(1).split(",").map(_.toDouble) LabeledPoint(label = y, features = Vectors.dense(features)) } model.trainOn(labeledStream) // 使用DStream的转换算子 val predictAndTrue = labeledStream.transform { rdd => val latestModel = model.latestModel() rdd.map { point => val predict = latestModel.predict(point.features) predict - point.label } } // 计算MSE predictAndTrue.foreachRDD { rdd => val mse = rdd.map(x => x * x).mean() val rmse = math.sqrt(mse) println(s"current batch, MSE: $mse, RMSE:$rmse") } ssc.start() ssc.awaitTermination() } }
Example 3
Source File: L9-9LogisticRegression.scala From prosparkstreaming with Apache License 2.0 | 5 votes |
package org.apress.prospark import org.apache.spark.SparkConf import org.apache.spark.SparkContext import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD import org.apache.spark.rdd.RDD import org.apache.spark.rdd.RDD.doubleRDDToDoubleRDDFunctions import org.apache.spark.streaming.Seconds import org.apache.spark.streaming.StreamingContext import org.apache.spark.mllib.classification.StreamingLogisticRegressionWithSGD object LogisticRegressionApp { def main(args: Array[String]) { if (args.length != 4) { System.err.println( "Usage: LogisticRegressionApp <appname> <batchInterval> <hostname> <port>") System.exit(1) } val Seq(appName, batchInterval, hostname, port) = args.toSeq val conf = new SparkConf() .setAppName(appName) .setJars(SparkContext.jarOfClass(this.getClass).toSeq) val ssc = new StreamingContext(conf, Seconds(batchInterval.toInt)) val substream = ssc.socketTextStream(hostname, port.toInt) .filter(!_.contains("NaN")) .map(_.split(" ")) .filter(f => f(1) != "0") val datastream = substream.map(f => Array(f(1).toDouble, f(2).toDouble, f(4).toDouble, f(5).toDouble, f(6).toDouble)) val walkingOrRunning = datastream.filter(f => f(0) == 4.0 || f(0) == 5.0).map(f => LabeledPoint(f(0), Vectors.dense(f.slice(1, 5)))) val test = walkingOrRunning.transform(rdd => rdd.randomSplit(Array(0.3, 0.7))(0)) val train = walkingOrRunning.transformWith(test, (r1: RDD[LabeledPoint], r2: RDD[LabeledPoint]) => r1.subtract(r2)).cache() val model = new StreamingLogisticRegressionWithSGD() .setInitialWeights(Vectors.zeros(4)) .setStepSize(0.0001) .setNumIterations(1) model.trainOn(train) model.predictOnValues(test.map(v => (v.label, v.features))).foreachRDD(rdd => println("MSE: %f".format(rdd .map(v => math.pow((v._1 - v._2), 2)).mean()))) ssc.start() ssc.awaitTermination() } }
Example 4
Source File: L9-1LinearRegression.scala From prosparkstreaming with Apache License 2.0 | 5 votes |
package org.apress.prospark import org.apache.spark.SparkConf import org.apache.spark.SparkContext import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD import org.apache.spark.rdd.RDD import org.apache.spark.rdd.RDD.doubleRDDToDoubleRDDFunctions import org.apache.spark.streaming.Seconds import org.apache.spark.streaming.StreamingContext object LinearRegressionApp { def main(args: Array[String]) { if (args.length != 4) { System.err.println( "Usage: LinearRegressionApp <appname> <batchInterval> <hostname> <port>") System.exit(1) } val Seq(appName, batchInterval, hostname, port) = args.toSeq val conf = new SparkConf() .setAppName(appName) .setJars(SparkContext.jarOfClass(this.getClass).toSeq) val ssc = new StreamingContext(conf, Seconds(batchInterval.toInt)) val substream = ssc.socketTextStream(hostname, port.toInt) .filter(!_.contains("NaN")) .map(_.split(" ")) .filter(f => f(1) != "0") val datastream = substream.map(f => Array(f(2).toDouble, f(3).toDouble, f(4).toDouble, f(5).toDouble, f(6).toDouble)) .map(f => LabeledPoint(f(0), Vectors.dense(f.slice(1, 5)))) val test = datastream.transform(rdd => rdd.randomSplit(Array(0.3, 0.7))(0)) val train = datastream.transformWith(test, (r1: RDD[LabeledPoint], r2: RDD[LabeledPoint]) => r1.subtract(r2)).cache() val model = new StreamingLinearRegressionWithSGD() .setInitialWeights(Vectors.zeros(4)) .setStepSize(0.0001) .setNumIterations(1) model.trainOn(train) model.predictOnValues(test.map(v => (v.label, v.features))).foreachRDD(rdd => println("MSE: %f".format(rdd .map(v => math.pow((v._1 - v._2), 2)).mean()))) ssc.start() ssc.awaitTermination() } }
Example 5
Source File: StreamingLinearRegressionExample.scala From sparkoscope with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.mllib import org.apache.spark.SparkConf // $example on$ import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD // $example off$ import org.apache.spark.streaming._ object StreamingLinearRegressionExample { def main(args: Array[String]): Unit = { if (args.length != 2) { System.err.println("Usage: StreamingLinearRegressionExample <trainingDir> <testDir>") System.exit(1) } val conf = new SparkConf().setAppName("StreamingLinearRegressionExample") val ssc = new StreamingContext(conf, Seconds(1)) // $example on$ val trainingData = ssc.textFileStream(args(0)).map(LabeledPoint.parse).cache() val testData = ssc.textFileStream(args(1)).map(LabeledPoint.parse) val numFeatures = 3 val model = new StreamingLinearRegressionWithSGD() .setInitialWeights(Vectors.zeros(numFeatures)) model.trainOn(trainingData) model.predictOnValues(testData.map(lp => (lp.label, lp.features))).print() ssc.start() ssc.awaitTermination() // $example off$ ssc.stop() } } // scalastyle:on println
Example 6
Source File: HandsOnLinRegStreaming.scala From Hands-On-Data-Analysis-with-Scala with MIT License | 5 votes |
package handson.example import org.apache.spark._ import org.apache.spark.streaming._ import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD object HandsOnLinRegStreaming { def main(args: Array[String]): Unit = { val conf = new SparkConf().setMaster("local[2]").setAppName("HandsOnLinRegStreaming") val ssc = new StreamingContext(conf, Seconds(10)) val numFeatures = 3 val model = new StreamingLinearRegressionWithSGD().setInitialWeights(Vectors.zeros(numFeatures)) val trainingData = ssc.textFileStream("file:/tmp/lin-reg-train-data").map(LabeledPoint.parse).cache() trainingData.print() // output training data for debug purpose val testData = ssc.textFileStream("file:/tmp/lin-reg-test-data").map(LabeledPoint.parse) model.trainOn(trainingData) model.predictOnValues(testData.map(lp => (lp.label, lp.features))).print() ssc.start() ssc.awaitTerminationOrTimeout(1000*60*3) // Wait for the computation to terminate (3 minutes) } }
Example 7
Source File: StreamingLinearRegressionExample.scala From multi-tenancy-spark with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.mllib import org.apache.spark.SparkConf // $example on$ import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD // $example off$ import org.apache.spark.streaming._ object StreamingLinearRegressionExample { def main(args: Array[String]): Unit = { if (args.length != 2) { System.err.println("Usage: StreamingLinearRegressionExample <trainingDir> <testDir>") System.exit(1) } val conf = new SparkConf().setAppName("StreamingLinearRegressionExample") val ssc = new StreamingContext(conf, Seconds(1)) // $example on$ val trainingData = ssc.textFileStream(args(0)).map(LabeledPoint.parse).cache() val testData = ssc.textFileStream(args(1)).map(LabeledPoint.parse) val numFeatures = 3 val model = new StreamingLinearRegressionWithSGD() .setInitialWeights(Vectors.zeros(numFeatures)) model.trainOn(trainingData) model.predictOnValues(testData.map(lp => (lp.label, lp.features))).print() ssc.start() ssc.awaitTermination() // $example off$ ssc.stop() } } // scalastyle:on println
Example 8
Source File: LinearRegression.scala From twitter-stream-ml with GNU General Public License v3.0 | 5 votes |
package com.giorgioinf.twtml.spark import org.apache.spark.{Logging, SparkConf, SparkContext} import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD import org.apache.spark.rdd.RDD import org.apache.spark.streaming.{Seconds, StreamingContext} import org.apache.spark.streaming.twitter.TwitterUtils object LinearRegression extends Logging { def main(args: Array[String]) { log.info("Parsing applications arguments") val conf = new ConfArguments() .setAppName("twitter-stream-ml-linear-regression") .parse(args.toList) log.info("Initializing session stats...") val session = new SessionStats(conf).open log.info("Initializing Spark Machine Learning Model...") MllibHelper.reset(conf) val model = new StreamingLinearRegressionWithSGD() .setNumIterations(conf.numIterations) .setStepSize(conf.stepSize) .setMiniBatchFraction(conf.miniBatchFraction) .setInitialWeights(Vectors.zeros(MllibHelper.numFeatures)) log.info("Initializing Spark Context...") val sc = new SparkContext(conf.sparkConf) log.info("Initializing Streaming Spark Context... {} sec/batch", conf.seconds) val ssc = new StreamingContext(sc, Seconds(conf.seconds)) log.info("Initializing Twitter stream...") val stream = TwitterUtils.createStream(ssc, None) .filter(MllibHelper.filtrate) .map(MllibHelper.featurize) .cache() log.info("Initializing prediction model...") val count = sc.accumulator(0L, "count") stream.foreachRDD({ rdd => if (rdd.isEmpty) log.debug("batch: 0") else { val realPred = rdd.map{ lb => (lb.label, Utils.round(model.latestModel.predict(lb.features))) } val batch = rdd.count count += batch val real = realPred.map(_._1) val pred = realPred.map(_._2) val realStdev = Utils.round(real.stdev) val predStdev = Utils.round(pred.stdev) val mse = Utils.round(realPred.map{case(v, p) => math.pow((v - p), 2)}.mean()) if (log.isDebugEnabled) { log.debug("count: {}", count) // batch, mse (training mean squared error) log.debug("batch: {}, mse: {}", batch, mse) log.debug("stdev (real, pred): ({}, {})", realStdev.toLong, predStdev.toLong) log.debug("value (real, pred): {} ...", realPred.take(10).toArray) } session.update(count.value, batch, mse, realStdev, predStdev, real.toArray, pred.toArray); } }) log.info("Initializing training model...") // training after prediction model.trainOn(stream) // Start the streaming computation ssc.start() log.info("Initialization complete.") ssc.awaitTermination() } }
Example 9
Source File: StreamingLinearRegression.scala From spark1.52 with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.mllib import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.{LabeledPoint, StreamingLinearRegressionWithSGD} import org.apache.spark.SparkConf import org.apache.spark.streaming.{Seconds, StreamingContext} object StreamingLinearRegression { def main(args: Array[String]) { if (args.length != 4) { System.err.println( "Usage: StreamingLinearRegression <trainingDir> <testDir> <batchDuration> <numFeatures>") System.exit(1) } val conf = new SparkConf().setMaster("local").setAppName("StreamingLinearRegression") //批次间隔 val ssc = new StreamingContext(conf, Seconds(args(2).toLong)) //LabeledPoint标记点是局部向量,向量可以是密集型或者稀疏型,每个向量会关联了一个标签(label) val trainingData = ssc.textFileStream(args(0)).map(LabeledPoint.parse) val testData = ssc.textFileStream(args(1)).map(LabeledPoint.parse) val model = new StreamingLinearRegressionWithSGD()//(SGD随机梯度下降) //initialWeights初始取值,默认是0向量 .setInitialWeights(Vectors.zeros(args(3).toInt)) model.trainOn(trainingData) model.predictOnValues(testData.map(lp => (lp.label, lp.features))).print() ssc.start() ssc.awaitTermination() } } // scalastyle:on println
Example 10
Source File: StreamingLinearRegressionExample.scala From Spark-2.3.1 with Apache License 2.0 | 5 votes |
// scalastyle:off println package org.apache.spark.examples.mllib import org.apache.spark.SparkConf // $example on$ import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD // $example off$ import org.apache.spark.streaming._ object StreamingLinearRegressionExample { def main(args: Array[String]): Unit = { if (args.length != 2) { System.err.println("Usage: StreamingLinearRegressionExample <trainingDir> <testDir>") System.exit(1) } val conf = new SparkConf().setAppName("StreamingLinearRegressionExample") val ssc = new StreamingContext(conf, Seconds(1)) // $example on$ val trainingData = ssc.textFileStream(args(0)).map(LabeledPoint.parse).cache() val testData = ssc.textFileStream(args(1)).map(LabeledPoint.parse) val numFeatures = 3 val model = new StreamingLinearRegressionWithSGD() .setInitialWeights(Vectors.zeros(numFeatures)) model.trainOn(trainingData) model.predictOnValues(testData.map(lp => (lp.label, lp.features))).print() ssc.start() ssc.awaitTermination() // $example off$ ssc.stop() } } // scalastyle:on println
Example 11
Source File: LogisticStreaming.scala From Apache-Spark-2x-Machine-Learning-Cookbook with MIT License | 5 votes |
package spark.ml.cookbook.chapter13 import org.apache.log4j.{Level, Logger} import org.apache.spark.mllib.classification.StreamingLogisticRegressionWithSGD import org.apache.spark.mllib.linalg.Vectors import org.apache.spark.mllib.regression.LabeledPoint import org.apache.spark.mllib.regression.StreamingLinearRegressionWithSGD import org.apache.spark.rdd.RDD import org.apache.spark.sql.{Row, SparkSession} import org.apache.spark.streaming.{Seconds, StreamingContext} import scala.collection.mutable.Queue object LogisticStreaming { def main(args: Array[String]) { Logger.getLogger("org").setLevel(Level.ERROR) Logger.getRootLogger.setLevel(Level.WARN) val spark = SparkSession .builder .master("local[*]") .appName("Logistic Streaming App") .config("spark.sql.warehouse.dir", ".") .getOrCreate() import spark.implicits._ val ssc = new StreamingContext(spark.sparkContext, Seconds(2)) val rawDF = spark.read .text("../data/sparkml2/chapter13/pima-indians-diabetes.data").as[String] val buf = rawDF.rdd.map(value => { val data = value.split(",") (data.init.toSeq, data.last) }) val lps = buf.map{ case (feature: Seq[String], label: String) => val featureVector = feature.map(_.toDouble).toArray[Double] LabeledPoint(label.toDouble, Vectors.dense(featureVector)) } val trainQueue = new Queue[RDD[LabeledPoint]]() val testQueue = new Queue[RDD[LabeledPoint]]() val trainingStream = ssc.queueStream(trainQueue) val testStream = ssc.queueStream(testQueue) val numFeatures = 8 val model = new StreamingLogisticRegressionWithSGD() .setInitialWeights(Vectors.zeros(numFeatures)) .setNumIterations(15) .setStepSize(0.5) .setMiniBatchFraction(0.25) model.trainOn(trainingStream) val result = model.predictOnValues(testStream.map(lp => (lp.label, lp.features))) result.map{ case (label: Double, prediction: Double) => (label, prediction) }.print() ssc.start() val Array(trainData, test) = lps.randomSplit(Array(.80, .20)) trainQueue += trainData Thread.sleep(4000) val testGroups = test.randomSplit(Array(.50, .50)) testGroups.foreach(group => { testQueue += group Thread.sleep(2000) }) ssc.stop() } }