简体   繁体   English

随机正态值的斯卡拉微风矩阵

[英]scala breeze matrix of random normal values

I want the same result that is obtained in python with 我想要在python中获得的相同结果

x=np.random.normal(0, 1, (n_samples, n_features))

I have tried: 我努力了:

import breeze.linalg._

object HelloWorld {
  def main(args: Array[String]) {
    println("Start")

    val n_samples = 5
    val n_features = 3

    val normal01 = breeze.stats.distributions.Gaussian(0, 1)
    val samples = normal01.sample(n_features*n_features)

    val X = DenseMatrix(n_samples, n_features,  samples) // return an error



    //print(X)
  }
}

Where is the error? 错误在哪里?

A simple alternative implementation: 一个简单的替代实现:

val normal01 = breeze.stats.distributions.Gaussian(0, 1)
DenseMatrix.rand(n_samples, n_features, normal01)

The .rand constructor accepts an optional random generator, which defaults to Uniform(0, 1) .rand构造函数接受一个可选的随机生成器,默认为Uniform(0,1)

Replace the matrix creation line with: 将矩阵创建行替换为:

val X = new DenseMatrix[Double](n_samples, n_features, samples.toArray)

and then fix the typo on the previous line. 然后在前一行上修正错字。

For some reason this constructor doesn't seem to be in the companion object, so you have to use the "new" keyword (this is probably a bug in Breeze, so you could file it as an issue). 由于某种原因,该构造函数似乎不在伴随对象中,因此您必须使用“ new”关键字(这可能是Breeze中的错误,因此可以将其作为问题提出)。 Also, you need to coerce "samples" into a regular Scala Array. 另外,您需要将“样本”强制转换为常规Scala数组。

This is a solution for np.random.normal in Python with a seed (same seed generates same ramdoms) 这是带种子的Python中np.random.normal的解决方案(相同的种子会产生相同的随机数)

implicit val randBasis: RandBasis = new RandBasis(new ThreadLocalRandomGenerator(new MersenneTwister(seed)))
val Gausian = breeze.stats.distributions.Gaussian(0.0, 1.0)
val R: DenseMatrix[Double] = DenseMatrix.rand(numRows, numColumns, Gausian)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM