• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala LabeledPoint类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala GraphStageLogic类代码示例发布时间:2022-05-23
下一篇:
Scala ArrayDeque类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap