当前位置: 首页>>代码示例>>Scala>>正文


Scala LabeledPoint类代码示例

本文整理汇总了Scala中org.apache.spark.ml.feature.LabeledPoint的典型用法代码示例。如果您正苦于以下问题:Scala LabeledPoint类的具体用法?Scala LabeledPoint怎么用?Scala LabeledPoint使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了LabeledPoint类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: mltest

//设置package包名称以及导入依赖的类
package spark.mltest

import org.apache.spark.ml.feature.LabeledPoint
import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by I311352 on 3/29/2017.
  */
class mltest {

}

object mltest extends App {
  val conf = new SparkConf().setAppName("mltest").setMaster("local[2]")
  val sc = new SparkContext(conf)
  val sQLContext = new SQLContext(sc)
  println("OK")

  val training = sc.parallelize(Seq(
    LabeledPoint
  ))
} 
开发者ID:compasses,项目名称:elastic-spark,代码行数:24,代码来源:mltest.scala

示例2: NeuralNetworkSpec

//设置package包名称以及导入依赖的类
package io.spinor.sparkdemo.mllib

import io.spinor.sparkdemo.data.MNISTData
import io.spinor.sparkdemo.util.DemoUtil
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature.LabeledPoint
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.SparkSession
import org.apache.spark.{SparkConf, SparkContext}
import org.scalatest.{FlatSpec, Matchers}
import org.slf4j.LoggerFactory


class NeuralNetworkSpec extends FlatSpec with DemoUtil with Matchers {
  val logger = LoggerFactory.getLogger(classOf[NeuralNetworkSpec])

  "Training on MNIST data" should " run" in {
    val sparkConf = new SparkConf()
    sparkConf.setAppName("NeuralNetworkDemo")
    sparkConf.setMaster("local[2]")

    val sparkContext = new SparkContext(sparkConf)
    val sparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
    val sqlContext = sparkSession.sqlContext
    import sqlContext.implicits._

    val mNISTData = new MNISTData()
    val trainingData = mNISTData.getTrainingData()
    val trainingPoints = sparkContext.parallelize(trainingData.map(entry => LabeledPoint(entry._2, Vectors.dense(entry._1)))).toDF()

    val classifier = new MultilayerPerceptronClassifier()
    classifier
      .setLayers(Array(784, 100))
      .setBlockSize(125)
      .setSeed(1234L)
      .setMaxIter(10)

    val model = classifier.fit(trainingPoints)

    val testData = mNISTData.getTestData()
    val testPoints = sparkContext.parallelize(testData.map(entry => {
    LabeledPoint(entry._2, Vectors.dense(entry._1))})).toDF()
    val result = model.transform(testPoints)
    val predictionAndLabels = result.select("prediction", "label")
    val evaluator = new MulticlassClassificationEvaluator().setMetricName("accuracy")

    logger.info("accuracy:" + evaluator.evaluate(predictionAndLabels))
  }
} 
开发者ID:arshadm,项目名称:spark-demo,代码行数:51,代码来源:NeuralNetworkSpec.scala

示例3: WatherScript

//设置package包名称以及导入依赖的类
package naivebayes

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.ml.classification.NaiveBayes
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.feature.LabeledPoint
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}

object WatherScript extends App {

  val conf = new SparkConf().setAppName("Simple Application")
  val sc = new SparkContext(conf)

  val spark = SparkSession
    .builder()
    .appName("Spark SQL basic example")
    .config("spark.some.config.option", "some-value")
    .getOrCreate()

  // For implicit conversions like converting RDDs to DataFrames
  import spark.implicits._

  val watherRaw: RDD[String] = sc.textFile("/Users/mateusz/Workspace/mllib/spark-naive-bayes/src/main/resources/wather-nums.csv")

  val dataRaw = watherRaw.map(_.split(";")).map { csv =>
    val label = csv.last.toDouble
    val point = csv.init.map(_.toDouble)
    (label, point)
  }

  val data: Dataset[LabeledPoint] = dataRaw
    .map { case (label, point) =>
      LabeledPoint(label, Vectors.dense(point))
    }.toDS()

  val Array(training: Dataset[LabeledPoint], test: Dataset[LabeledPoint]) = data.randomSplit(Array(0.7, 0.3), seed = 1234L)

  val model = new NaiveBayes()
    .setModelType("multinomial")
    .fit(training)

  val predictions = model.transform(test)
  predictions.show()

  val evaluator = new MulticlassClassificationEvaluator()
    .setLabelCol("label")
    .setPredictionCol("prediction")
    .setMetricName("accuracy")
  val accuracy = evaluator.evaluate(predictions)

  println("Test set accuracy = " + accuracy)
} 
开发者ID:mateuszjancy,项目名称:spark-naive-bayes,代码行数:55,代码来源:WatherScript.scala


注:本文中的org.apache.spark.ml.feature.LabeledPoint类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。