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

Scala DenseMatrix类代码示例

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

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



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

示例1: rddvector

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

import breeze.linalg.{DenseMatrix, DenseVector}
import org.apache.spark.{SparkConf, SparkContext}
import org.slf4j.LoggerFactory
import spark.RecommendationExample.getClass

/**
  * Created by I311352 on 4/5/2017.
  */
object rddvector extends App {
  val LOG = LoggerFactory.getLogger(getClass)

  val conf = new SparkConf().setAppName("vector").setMaster("local[2]")
  val sc = new SparkContext(conf)
  val data = sc.textFile("data/testdata.txt")
  println(data.take(10).toList)

  val vectorRDD = data.map(value => {
    val columns = value.split(",").map(value => value.toDouble)
    new DenseVector(columns)
  })

  println(vectorRDD.take(100).toList)

  // multiply each row by a constant vector
  val constant = 5.0
  val broadcastConstant = sc.broadcast(constant)
  val scaledRDD = vectorRDD.map(row => {
    row :* broadcastConstant.value
  })

  println(scaledRDD.take(10).toList)

  val scaledRDDByPartition = vectorRDD.glom().map((value:Array[DenseVector[Double]]) => {
    val arrayValues = value.map(denseVector => denseVector.data).flatten
    val denseMatrix = new DenseMatrix[Double](value.length,value(0).length,arrayValues)
    denseMatrix :*= broadcastConstant.value
    denseMatrix.toDenseVector
  })

  println(scaledRDDByPartition.take(10).toList)
} 
开发者ID:compasses,项目名称:elastic-spark,代码行数:44,代码来源:rddvector.scala


示例2: Evaluation

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

import breeze.linalg.{DenseMatrix, DenseVector, sum}
import regression.Regressor


object Evaluation {
  def confusion(lr: Regressor, data: Iterable[(DenseVector[Double], Double)]): DenseMatrix[Double] = {
    val confusion = DenseMatrix.zeros[Double](2, 2)

    data.map { case (x, y) =>
      (y.toInt, if (lr.predict(x) > 0.5) 1 else 0)
    } foreach { case (truth, predicted) =>
      confusion(truth, predicted) += 1.0
    }

    confusion
  }

  def printConfusionMtx(confusion: DenseMatrix[Double]): Unit = {
    val negatives = confusion(0, 0) + confusion(0, 1)
    val positives = confusion(1, 0) + confusion(1, 1)
    val total = sum(confusion)

    val falseNegatives = confusion(1, 0)
    val falsePositives = confusion(0, 1)
    val accuracy = (confusion(0, 0) + confusion(1, 1)) / total

    println("============= Stats =============\n")

    println(f"Positive examples: $positives%1.0f")
    println(f"Negative examples: $negatives%1.0f")
    println(f"Total: $total%1.0f")
    println(f"Pos/Neg ratio: ${positives/negatives}%1.2f")

    println("\n============= Results =============\n")

    println("Confusion Matrix:")
    println(confusion)
    println(f"Accuracy: ${accuracy * 100}%2.2f%%")
    println(f"False positives: ${falsePositives * 100 / negatives}%2.2f%%")
    println(f"False negatives: ${falseNegatives * 100 / positives}%2.2f%%")
  }
} 
开发者ID:agolovenko,项目名称:ml-tools,代码行数:45,代码来源:Evaluation.scala


示例3: ZFLSH

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

import breeze.linalg.DenseMatrix
import org.apache.spark.mllib.linalg
import org.apache.spark.mllib.linalg.{Vector, Vectors}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
import AccurateML.blas.ZFBLAS


class ZFLSH(
             n: Int,
             m: Int) {
  val normal01 = breeze.stats.distributions.Gaussian(0, 1)
  val nmat = DenseMatrix.rand(m, n, normal01)

  def hashVector(vector: linalg.Vector): String = {

    val r = new Array[Int](n)
    for (i <- 0 until n) {
      val mc = nmat(::, (i))
      val ans = ZFBLAS.dot(vector, Vectors.dense(mc.toArray))
      if (ans > 0)
        r(i) = 1
    }
    r.mkString("")
  }

  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("test lsh")
    val sc = new SparkContext(conf)
    val numBits = 4
    val numFeatures = 1000
    val lsh = new ZFLSH(numBits, numFeatures)
    val data: RDD[Vector] = sc.objectFile("") //eg, the first element in data is dfirst=Vector(1.0,2.0,...,1000.0)
    val mapData:RDD[(String,Vector)]=data.map(vec=>(lsh.hashVector(vec),vec))
    //eg,the first element in mapData mdfirst=Tuple2("1010",Vector(1.0,2.0,...,100.0))
    //"1010" is the sketch of dfirst=Vector(1.0,2.0,...,1000.0)
    //the instances with the same sketch will belong to the same cluster

  }

} 
开发者ID:harryandlina,项目名称:AccurateML,代码行数:44,代码来源:ZFLSH.scala


示例4: BreezeMatrixOperations

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

import breeze.linalg.{DenseMatrix, _}
import breeze.numerics._



object BreezeMatrixOperations {

  def main(args: Array[String]) {
    val a = DenseMatrix((1,2),(3,4))
    val b = DenseMatrix((2,2),(2,2))
    val c = a + b
    println("a: \n" + a)
    println("b: \n" + b)
    println("a + b : \n" + c)
    val d = a*b
    println("Dot product a*b : \n" + d)

    val e = a :+= 1
    println("Inplace Addition : a :+= 1\n" + e)

    //a:*= 2.0
    //println("Inplace Multiplication : a :*= 2.0\n" + a :*=2.0)

    val f = a :< b
    println("a :< b \n" + f)
    val g = DenseMatrix((1.1, 1.2), (3.9, 3.5))
    println("g: \n" + g)
    val gCeil =ceil(g)
    println("ceil(g)\n " + gCeil)

    val gFloor =floor(g)
    println("floor(g)\n" + gFloor)

    val sumA = sum(a)
    println("sum(a):\n" + sumA)
    println("a.max:\n" + a.max)
    println("argmax(a):\n" + argmax(a))

  }

} 
开发者ID:PacktPublishing,项目名称:Machine-Learning-with-Spark-Second-Edition,代码行数:44,代码来源:BreezeMatrixOperations.scala


示例5: SelfOrganizingMapSpec

//设置package包名称以及导入依赖的类
package io.flatmap.ml.som

import breeze.numerics.closeTo
import breeze.linalg.DenseMatrix
import io.flatmap.ml.som.SelfOrganizingMap.Shape
import org.apache.spark.mllib.linalg.DenseVector
import org.apache.spark.mllib.random.RandomRDDs
import org.scalatest._
import util.{FakeDecayFunction, FakeMetrics, FakeNeighborhoodKernel, TestSparkContext}

class SelfOrganizingMapSpec extends FlatSpec with Matchers with BeforeAndAfterEach with TestSparkContext {

  def SOM(width: Int, height: Int) =
    new SelfOrganizingMap with FakeNeighborhoodKernel with FakeDecayFunction with FakeMetrics {
      override val shape: Shape = (width, height)
      override val learningRate: Double = 0.1
      override val sigma: Double = 0.2
    }

  "instantiation" should "create a SOM with codebook of zeros" in {
    val som = SOM(6, 6)
    som.codeBook should === (DenseMatrix.fill[Array[Double]](6, 6)(Array.emptyDoubleArray))
  }

  "initialize" should "copy random data points from RDD into codebook" in {
    val data = RandomRDDs.normalVectorRDD(sparkSession.sparkContext, numRows = 512L, numCols = 3)
    val som = SOM(6, 6)
    som.initialize(data).codeBook should !== (DenseMatrix.fill[Array[Double]](6, 6)(Array.emptyDoubleArray))
  }

  "winner" should "return best matching unit (BMU)" in {
    val som = SOM(6, 6)
    som.codeBook.keysIterator.foreach { case (x, y) => som.codeBook(x, y) = Array(0.2, 0.2, 0.2) }
    som.codeBook(3, 3) = Array(0.3, 0.3, 0.3)
    som.winner(new DenseVector(Array(2.0, 2.0, 2.0)), som.codeBook) should equal ((3, 3))
    som.winner(new DenseVector(Array(0.26, 0.26, 0.26)), som.codeBook) should equal ((3, 3))
  }

  "winner" should "return last best matching unit (BMU) index in case of multiple BMUs" in {
    val som = SOM(6, 6)
    som.codeBook.keysIterator.foreach { case (x, y) => som.codeBook(x, y) = Array(0.2, 0.2, 0.2) }
    som.codeBook(3, 3) = Array(0.3, 0.3, 0.3)
    som.winner(new DenseVector(Array(0.25, 0.25, 0.25)), som.codeBook) should equal ((5, 5))
  }

  "classify" should "return the best matching unit along with Euclidean distance" in {
    val som = SOM(6, 6)
    som.codeBook.keysIterator.foreach { case (x, y) => som.codeBook(x, y) = Array(0.2, 0.2, 0.2) }
    som.codeBook(3, 3) = Array(0.3, 0.3, 0.3)
    val (bmu, distance) = som.classify(new DenseVector(Array(0.26, 0.26, 0.26)))
    bmu should === ((3, 3))
    assert(closeTo(distance, 0.06, relDiff = 1e-2))
  }

} 
开发者ID:ShokuninSan,项目名称:som,代码行数:56,代码来源:SelfOrganizingMapSpec.scala


示例6: FloatMatrixUtils

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

import breeze.linalg.{DenseMatrix, convert, max, min}
import breeze.plot._

object FloatMatrixUtils {
  def visualize(mat: DenseMatrix[Float]): Figure = {
    val f2 = Figure()
    f2.height = mat.rows
    f2.width = mat.cols
    f2.subplot(0) += image(convert(mat, Double), GradientPaintScale(min(mat), max(mat), PaintScale.BlackToWhite))
    f2.subplot(0).yaxis.setInverted(true)
    f2
  }

  
} 
开发者ID:Vaishaal,项目名称:ckm,代码行数:18,代码来源:FloatMatrixUtils.scala


示例7: ScanDir

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

import breeze.linalg.DenseMatrix
import nak.cluster.{DBSCAN, GDBSCAN, Kmeans}

import scala.collection.mutable.ArrayBuffer


object ScanDir {
  def apply(
             iter: Iterable[(Double, Double)],
             epsilon: Double,
             minPoints: Int
           ) = {

    val (xarr, yarr) = iter
      .foldLeft((new ArrayBuffer[Double](), new ArrayBuffer[Double]())) {
        case ((xarr, yarr), (x, y)) => {
          xarr += x
          yarr += y
          (xarr, yarr)
        }
      }

    val matrix = new DenseMatrix[Double](xarr.size, 2, (xarr ++ yarr).toArray)

    val dbScan = new GDBSCAN(
      DBSCAN.getNeighbours(epsilon = epsilon,
        distance = Kmeans.euclideanDistance),
      DBSCAN.isCorePoint(minPoints = minPoints))

    val clusters = dbScan cluster matrix
    clusters.flatMap(cluster => {
      val points = cluster
        .points
        .map(point => {
          val denseVector = point.value
          (denseVector(0), denseVector(1))
        })
      DirDist(points, minPoints)
    })
  }
} 
开发者ID:mraad,项目名称:spark-std-dist,代码行数:44,代码来源:ScanDir.scala


示例8: ScanDist

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

import breeze.linalg.DenseMatrix
import nak.cluster.{DBSCAN, GDBSCAN, Kmeans}

import scala.collection.mutable.ArrayBuffer
import scala.math._


object ScanDist {
  def apply(
             iter: Iterable[(Double, Double)],
             epsilon: Double,
             minPoints: Int
           ): Seq[StdDist] = {

    val (xarr, yarr) = iter
      .foldLeft((new ArrayBuffer[Double](), new ArrayBuffer[Double]())) {
        case ((xarr_, yarr_), (x, y)) => {
          xarr_ += x
          yarr_ += y
          (xarr_, yarr_)
        }
      }

    val matrix = new DenseMatrix[Double](xarr.size, 2, (xarr ++ yarr).toArray)

    val dbScan = new GDBSCAN(
      DBSCAN.getNeighbours(epsilon = epsilon,
        distance = Kmeans.euclideanDistance),
      DBSCAN.isCorePoint(minPoints = minPoints))

    val clusters = dbScan cluster matrix
    clusters.map(cluster => {
      val (ox, oy) = cluster.points.foldLeft((OnlineVar(), OnlineVar())) {
        case ((ox, oy), point) => {
          val denseVector = point.value
          ox += denseVector(0)
          oy += denseVector(1)
          (ox, oy)
        }
      }
      val sd = sqrt(ox.variance + oy.variance)
      StdDist(ox.mu, oy.mu, sd)
    })
  }

} 
开发者ID:mraad,项目名称:spark-std-dist,代码行数:49,代码来源:ScanDist.scala


示例9: PassiveAggressiveMultiModelEvaluation

//设置package包名称以及导入依赖的类
package hu.sztaki.ilab.ps.test.utils

import breeze.linalg.{DenseMatrix, SparseVector}
import hu.sztaki.ilab.ps.passive.aggressive.algorithm.PassiveAggressiveMulticlassAlgorithm
import org.slf4j.LoggerFactory

class PassiveAggressiveMultiModelEvaluation

object PassiveAggressiveMultiModelEvaluation {

  private val log = LoggerFactory.getLogger(classOf[PassiveAggressiveMultiModelEvaluation])

  def accuracy(model: DenseMatrix[Double], testLines: Traversable[(SparseVector[Double], Option[Int])],
               featureCount: Int, pac: PassiveAggressiveMulticlassAlgorithm): Double = {

    var hit = 0
    var cnt = 0
    testLines.foreach{case(vector, label) => label match {
      case Some(l) =>
      if (pac.predict(vector, model) == l) hit += 1
      cnt += 1
      case _ => throw new IllegalStateException("Labels should not be missing.")
    }}
    val percent = (hit.toDouble / cnt) * 100
    percent
  }

} 
开发者ID:gaborhermann,项目名称:flink-parameter-server,代码行数:29,代码来源:PassiveAggressiveMultiModelEvaluation.scala


示例10: QuadraticObjectiveFunction

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

import breeze.linalg.{DenseMatrix, DenseVector}
import MatrixUtils._


class QuadraticObjectiveFunction(

        override val dim:Int,
        val r:Double,
        val a:DenseVector[Double],
        val P:DenseMatrix[Double]
)
extends ObjectiveFunction(dim) {

    if(a.length!=dim){
        val msg = "Vector a must be of dimension "+dim+" but length(a) "+a.length
        throw new IllegalArgumentException(msg)
    }
    if(!(P.rows==dim & P.cols==dim)) {

        val msg = "Matrix P must be square of dimension "+dim+" but is "+P.rows+"x"+P.cols
        throw new IllegalArgumentException(msg)
    }
    checkSymmetric(P,1e-13)

    def valueAt(x:DenseVector[Double]) = { checkDim(x); r + (a dot x) + (x dot (P*x))/2 }
    def gradientAt(x:DenseVector[Double]) = { checkDim(x); a+P*x }
    def hessianAt(x:DenseVector[Double]) = { checkDim(x); P }

} 
开发者ID:spyqqqdia,项目名称:cvx,代码行数:32,代码来源:QuadraticObjectiveFunction.scala


示例11: hinge

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

import breeze.linalg.{*, DenseMatrix, DenseVector, norm}


  def hinge(xTr: DenseMatrix[Double],
               yTr: DenseVector[Int]): DenseVector[Double] = {
    val doubleYTR = new DenseVector(yTr.toArray.map(_.toDouble))
    val YWX = doubleYTR *:* (xTr(*, ::) dot w)
    val YX = xTr(::, *) *:* doubleYTR
    val delta = (DenseVector.ones[Double](YWX.length) - YWX).map(x => if (x <= 0) 0.0 else 1.0)
    val gradient = (YX(::, *) dot delta) * -1.0
    reg match {
      case Some(reg) => gradient.t + reg.regGradient(w)
      case None => gradient.t
    }
  }

} 
开发者ID:ChenJesse,项目名称:Spectrum,代码行数:20,代码来源:SVMClassifier.scala


示例12: adagrad

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

import breeze.linalg.{*, DenseMatrix, DenseVector, norm}


  def adagrad(lossFunc: ((DenseMatrix[Double], DenseVector[Int]) => DenseVector[Double]),
                      alpha: Double, maxiter: Int, delta: Double,
                      xTr: DenseMatrix[Double], yTr: DenseVector[Int]): Unit = {
    var z = DenseVector.zeros[Double](dimension)
    for (_ <- 1 until maxiter) {
      val gradient = lossFunc(xTr, yTr)
      z = z + gradient.map {x => x * x}
      val zEps = z :+= 0.0001
      val alphaGradient = gradient :*= alpha
      val newW = w - alphaGradient /:/ zEps.map(x => Math.sqrt(x))
      if (norm(gradient) < delta) return
      w = newW
    }
  }
} 
开发者ID:ChenJesse,项目名称:Spectrum,代码行数:21,代码来源:LinearClassifier.scala


示例13: logistic

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

import breeze.linalg.{*, Axis, DenseMatrix, DenseVector, norm, sum}
import breeze.numerics.exp


  def logistic(xTr: DenseMatrix[Double],
               yTr: DenseVector[Int]): DenseVector[Double] = {
    val doubleYTR = new DenseVector(yTr.toArray.map(_.toDouble))
    val YWX = doubleYTR *:* (xTr(*, ::) dot w)
    var eToTheYWX = exp.inPlace(YWX)
    val numerator = xTr(::, *) *:* doubleYTR
    val denominator = eToTheYWX :+= 1.0
    val gradient = (sum(numerator(::, *) /:/ denominator, Axis._0) * -1.0).t
    reg match {
      case Some(reg) => gradient + reg.regGradient(w)
      case None => gradient
    }
  }
} 
开发者ID:ChenJesse,项目名称:Spectrum,代码行数:21,代码来源:LogisticRegressionClassifier.scala


示例14: LinearClassifierWrapper

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

import breeze.linalg.{DenseMatrix, DenseVector}
import models.Vectorizable



class LinearClassifierWrapper[T <: Vectorizable](val classifier: LinearClassifier) {
  def train(xTr: Seq[T], yTr: Seq[Label]) = {
    val xTrMatrix = seqToMatrix(xTr)
    val yTrVector = seqLabelToVector(yTr)
    classifier.train(xTrMatrix, yTrVector)
  }

  def classify(xTe: Seq[T]): Seq[Label] =
    classifier.classify(seqToMatrix(xTe)).toArray.map(y => BinaryLabel.toLabel(y))

  def test(xTe: Seq[T], yTe: Seq[Label]): Double = {
    classifier.test(seqToMatrix(xTe), seqLabelToVector(yTe))
  }

  def seqToMatrix(seq: Seq[T]): DenseMatrix[Double] = {
    val vectors = seq.map(_.vectorize())
    DenseMatrix.tabulate(vectors.size, vectors.head.length) { case (i, j) => vectors(i).valueAt(j) }
  }

  def seqLabelToVector(seq: Seq[Label]) = DenseVector(seq.map(y => BinaryLabel.toInt(y)).toArray)

  def loadParams(w: DenseVector[Double], b: Double): Unit = {
    try {
      classifier.asInstanceOf[LinearClassifier].loadParams(w, b)
    } catch {
      case _: Exception =>
    }
  }
} 
开发者ID:ChenJesse,项目名称:Spectrum,代码行数:37,代码来源:LinearClassifierWrapper.scala


示例15: NaiveBayesClassifierSpec

//设置package包名称以及导入依赖的类
import breeze.linalg.{DenseMatrix, DenseVector}
import classifiers.NaiveBayesClassifier
import org.scalatestplus.play.PlaySpec


class NaiveBayesClassifierSpec extends PlaySpec {
  "Naive bayes classifier"  should {

    "Train w and b correctly and classify correctly" in {
      val classifier = new NaiveBayesClassifier(6)

      val xTr = DenseMatrix(
        (1.0, 1.0, 1.0, 0.0, 0.0, 0.0),
        (1.0, 0.0, 1.0, 0.0, 0.0, 0.0),
        (1.0, 1.0, 0.0, 0.0, 0.0, 0.0),
        (1.0, 1.0, 1.0, 1.0, 0.0, 0.0),
        (1.0, 1.0, 1.0, 0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 1.0, 1.0, 1.0),
        (0.0, 0.0, 0.0, 1.0, 0.0, 1.0),
        (0.0, 0.0, 1.0, 0.0, 1.0, 1.0),
        (0.0, 0.0, 0.0, 1.0, 1.0, 1.0),
        (0.0, 1.0, 1.0, 0.0, 1.0, 1.0)
      )

      val yTr = DenseVector(1, 1, 1, 1, 1, -1, -1, -1, -1, -1)

      val xTe = DenseMatrix(
        (1.0, 1.0, 0.0, 0.0, 0.0, 0.0),
        (0.0, 0.0, 0.0, 0.0, 1.0, 1.0),
        (1.0, 1.0, 1.0, 1.0, 0.0, 0.0)
      )

      classifier.train(xTr, yTr)
      val y = classifier.classify(xTe).toArray
      y.length mustBe 3
      y(0) mustBe 1
      y(1) mustBe -1
      y(2) mustBe 1

      val trainingError = classifier.test(xTr, yTr)
      trainingError mustBe 0.0
    }
  }
} 
开发者ID:ChenJesse,项目名称:Spectrum,代码行数:45,代码来源:NaiveBayesClassifierSpec.scala


示例16: Param

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

object Param {
  import breeze.linalg.{DenseVector,DenseMatrix}
  type DVec = DenseVector[Double]
  type DMat = DenseMatrix[Double]

  abstract class Generic(a:Any,R:Any,f:Any,Q:Any,m:Any,C:Any)

  // Univariate with Unknown V
  // S0 = d0/n0
  case class UniDF(
    a:DVec=DenseVector.zeros[Double](0),R:DMat=DenseMatrix.zeros[Double](0,0),
    f:Double=0.0,Q:Double=0.0,
    m:DVec,C:DMat,
    n:Double=1,S:Double=1) extends Generic(a,R,f,Q,m,C)

  // Univariate
  case class Uni(
    a:DVec,R:DMat,
    f:Double,Q:Double,
    m:DVec,C:DMat) extends Generic(a,R,f,Q,m,C)

  
} 
开发者ID:luiarthur,项目名称:dlmScala,代码行数:26,代码来源:Param.scala


示例17: Metropolis

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

object Metropolis {

  private[Metropolis] trait GenericMetropolis {
    // To Implement:
    type State
    type Cov
    def rnorm(mu:State,sig:Cov): State

    // Pre-implemented:
    def update(curr:State, ll:State=>Double, lp:State=>Double,
               candSig:Cov): State = {
      def logLikePlusLogPrior(x:State) = ll(x) + lp(x)
      val cand = rnorm(curr,candSig)
      val u = math.log(scala.util.Random.nextDouble)
      val p = logLikePlusLogPrior(cand) - 
              logLikePlusLogPrior(curr)

      if (p > u) cand else curr
    }
  }

  object Univariate extends GenericMetropolis {
    type State = Double
    type Cov = Double
    def rnorm(x: State, sig:Cov) =
      breeze.stats.distributions.Gaussian(x,sig).sample
  }

  object Multivariate extends GenericMetropolis {
    import breeze.linalg.{DenseMatrix, DenseVector}
    type State = DenseVector[Double]
    type Cov = DenseMatrix[Double]
    def rnorm(x: State, cov:Cov) =
      breeze.stats.distributions.MultivariateGaussian(x,cov)
        .sample
  }

} 
开发者ID:luiarthur,项目名称:dlmScala,代码行数:41,代码来源:Metropolis.scala


示例18: f1scores

//设置package包名称以及导入依赖的类
package io.citrine.lolo.stats.metrics

import breeze.linalg.{DenseMatrix, sum}


  def f1scores(predictedVsActual: Seq[(Vector[Any], Any, Any)]): Double = {
      val labels = predictedVsActual.map(_._3).distinct
      val index = labels.zipWithIndex.toMap
      val numLabels = labels.size
      val confusionMatrix = DenseMatrix.zeros[Int](numLabels, numLabels)
      predictedVsActual.foreach(p => confusionMatrix(index(p._2), index(p._3)) += 1)
      val f1scores = labels.indices.map { i =>
        val actualPositive: Double = sum(confusionMatrix(::, i))
        val predictedPositive: Double = sum(confusionMatrix(i, ::))
        val precision = if (predictedPositive > 0) confusionMatrix(i, i) / predictedPositive else 1.0
        val recall = if (actualPositive > 0) confusionMatrix(i, i) / actualPositive else 1.0
        if (precision > 0.0 && recall > 0.0) {
          2.0 * precision * recall / (precision + recall) * actualPositive
        } else {
          0.0
        }
      }
      f1scores.sum / predictedVsActual.size
  }
} 
开发者ID:CitrineInformatics,项目名称:lolo,代码行数:26,代码来源:ClassificationMetrics.scala


示例19: fromFile2Data

//设置package包名称以及导入依赖的类
package com.xuanyuansen.algo.application

import breeze.linalg.DenseMatrix
import com.xuanyuansen.algo.{ simpleLossLayer, LstmNeuralNetwork }


  def fromFile2Data(filePath: String, dict: Map[String, DenseMatrix[Double]]): Seq[(Seq[DenseMatrix[Double]], Seq[DenseMatrix[Double]])] = {
    val out = scala.io.Source.fromFile(filePath).getLines().map {
      r =>
        val line = r.split("\t")
        val data = line
          .apply(1)
          .split(",")
          .map {
            k => dict.getOrElse(k, dict.get("UNKNOWN").get)
          }.toSeq

        val label = if (line.head.toInt == 1)
          new Array[Int](data.length).map { r => DenseMatrix((1.0, 0.0)).t }
        else
          new Array[Int](data.length).map { r => DenseMatrix((0.0, 1.0)).t }
        label.toSeq -> data
    }
    out.toSeq
  }

  def loadDict(filePath: String, wordDimension: Int): Map[String, DenseMatrix[Double]] = {
    scala.io.Source.fromFile(filePath).getLines().map {
      r =>
        val line = r.split("\t")
        val vec = DenseMatrix.create(wordDimension, 1, line
          .apply(1)
          .split(",")
          .slice(0, wordDimension)
          .map { k =>
            k.toDouble
          })
        line.head -> vec
    }.toMap

  }

  def main(args: Array[String]) {
    val simpleLSTM = new LstmNeuralNetwork(128, Seq(64, 2), 2, new simpleLossLayer)
    val dict = this.loadDict("dict.data", wordDimension = 128)
    val data = this.fromFile2Data("train.file", dict)

    for (idx <- 0 to data.length) {
      val currData = data.apply(idx)

      val loss = simpleLSTM.multilayer_backward_propagation(currData._2, currData._1)
      println(loss)
      simpleLSTM.LstmParams.foreach {
        k => k.update_param_adadelta(0.95)
      }
    }

  }
} 
开发者ID:RNNPredict,项目名称:LSTM-scala-,代码行数:60,代码来源:BehaviorModel.scala


示例20: ClusteringSpec

//设置package包名称以及导入依赖的类
package com.github.log0ymxm.mapper.clustering

import breeze.linalg.DenseMatrix

import org.scalatest._

class ClusteringSpec extends FunSuite {
  val distances: DenseMatrix[Double] = new DenseMatrix(8, 8, Array(
    0.00, 1.00, 0.26, 1.00, 0.38, 1.00, 0.58, 0.16,
    1.00, 0.00, 0.36, 0.29, 1.00, 0.32, 1.00, 0.19,
    0.26, 0.36, 0.00, 0.17, 1.00, 1.00, 0.40, 0.34,
    1.00, 0.29, 0.17, 0.00, 1.00, 1.00, 0.52, 1.00,
    0.38, 1.00, 1.00, 1.00, 0.00, 0.35, 1.00, 0.37,
    1.00, 0.32, 1.00, 1.00, 0.35, 0.00, 0.93, 0.28,
    0.58, 1.00, 0.40, 0.52, 1.00, 0.93, 0.00, 1.00,
    0.16, 0.19, 0.34, 1.00, 0.37, 0.28, 1.00, 0.00
  ))

  val expectedLinkage = Seq(
    (0, 7, 0.16, 2),
    (2, 3, 0.17, 2),
    (1, 7, 0.19, 3),
    (0, 2, 0.26, 5),
    (5, 7, 0.28, 6),
    (4, 5, 0.35, 7),
    (2, 6, 0.4, 8)
  )

  test("single linkage results") {
    val linkage = SingleLinkage(distances)
    assert(linkage(0) == (0, 7, 0.16, 2))
    assert(linkage(1) == (2, 3, 0.17, 2))
    assert(linkage(2) == (1, 7, 0.19, 3))
    assert(linkage(3) == (0, 2, 0.26, 5))
    assert(linkage(4) == (5, 7, 0.28, 6))
    assert(linkage(5) == (4, 5, 0.35, 7))
    assert(linkage(6) == (2, 6, 0.4, 8))
  }

  test("fcluster results") {
    assert(SingleLinkage.fcluster(expectedLinkage, 1) == Seq(0, 0, 0, 0, 0, 0, 0, 0))
    assert(SingleLinkage.fcluster(expectedLinkage, 2) == Seq(0, 0, 0, 0, 0, 0, 1, 0))
    assert(SingleLinkage.fcluster(expectedLinkage, 3) == Seq(0, 0, 0, 0, 1, 0, 2, 0))
    assert(SingleLinkage.fcluster(expectedLinkage, 4) == Seq(0, 0, 0, 0, 1, 2, 3, 0))
    assert(SingleLinkage.fcluster(expectedLinkage, 5) == Seq(0, 0, 1, 1, 2, 3, 4, 0))
    assert(SingleLinkage.fcluster(expectedLinkage, 6) == Seq(0, 1, 2, 2, 3, 4, 5, 0))
    assert(SingleLinkage.fcluster(expectedLinkage, 7) == Seq(0, 1, 2, 3, 4, 5, 6, 0))
    assert(SingleLinkage.fcluster(expectedLinkage, 8) == Seq(0, 1, 2, 3, 4, 5, 6, 7))
  }

} 
开发者ID:log0ymxm,项目名称:spark-mapper,代码行数:52,代码来源:SingleLinkageSpec.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala ClassUtils类代码示例发布时间:2022-05-23
下一篇:
Scala MultivariateGaussian类代码示例发布时间: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