本文介绍了Scala简单直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如对于给定的Array[Double]
val a = Array.tabulate(100){ _ => Random.nextDouble * 10 }
使用n
箱计算直方图的简单方法是什么?
what is a simple approach to calculate a histogram with n
bins ?
推荐答案
与@ om-nom-nom的答案非常相似的值准备,但是使用partition
的直方图方法非常小,
A very similar preparation of values as in @om-nom-nom 's answer, yet the histogram method quite small by using 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简单直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!