gpt4 book ai didi

Scala简单直方图

转载 作者:行者123 更新时间:2023-12-04 00:47:02 25 4
gpt4 key购买 nike

对于给定的 Array[Double] ,例如

val a = Array.tabulate(100){ _ => Random.nextDouble * 10 }

使用 n 计算直方图的简单方法是什么?垃圾箱?

最佳答案

与@om-nom-nom 的答案非常相似的值准备,但使用 partition 的直方图方法非常小,

case class Distribution(nBins: Int, data: List[Double]) {
require(data.length > nBins)

val Epsilon = 0.000001
val (max,min) = (data.max,data.min)
val binWidth = (max - min) / nBins + Epsilon
val bounds = (1 to nBins).map { x => min + binWidth * x }.toList

def histo(bounds: List[Double], data: List[Double]): List[List[Double]] =
bounds match {
case h :: Nil => List(data)
case h :: t => val (l,r) = data.partition( _ < h) ; l :: histo(t,r)
}

val histogram = histo(bounds, data)
}

那么对于
val data = Array.tabulate(100){ _ => scala.util.Random.nextDouble * 10 }
val h = Distribution(5, data.toList).histogram

所以
val tabulated = h.map {_.size}

关于Scala简单直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24536215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com