Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala simple histogram

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 ?

like image 729
elm Avatar asked Jul 02 '14 16:07

elm


1 Answers

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)
like image 144
MDman Avatar answered Nov 08 '22 21:11

MDman