For a given Array[Double]
, for instance
val a = Array.tabulate(100){ _ => Random.nextDouble * 10 }
what is a simple approach to calculate a histogram with n
bins ?
How about this:
val num_bins = 20
val mx = a.max.toDouble
val mn = a.min.toDouble
val hist = a
.map(x=>(((x.toDouble-mn)/(mx-mn))*num_bins).floor.toInt)
.groupBy(x=>x)
.map(x=>x._1->x._2.size)
.toSeq
.sortBy(x=>x._1)
.map(x=>x._2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With