How can I compute a histogram in Haskell?

回眸只為那壹抹淺笑 提交于 2021-02-05 08:15:28

问题


I found Statistics.Sample.Histogram, but I can't seem to use it. If I want to be able to bin a list into four categories, I expect to be able to do something like this:

import Statistics.Sample.Histogram
histogram 4 [1, 2, 9, 9, 9, 9, 10, 11, 20]

But it gives me the error "non type-variable argument in the constraint," which I don't understand at all. What am I doing wrong?


回答1:


histogram takes a Vector of values, not a list. You can use Data.Vector's fromList function to convert your list into a Vector:

import qualified Statistics.Sample.Histogram as S
import qualified Data.Vector as V

main :: IO ()
main = do
    let xs = V.fromList [1, 2, 9, 9, 9, 9, 10, 11, 20]
        bins = 4
        (lowerbounds, sizes) = S.histogram bins xs
    print $ V.toList lowerbounds
    print $ V.toList sizes

The result is a pair of Vectors holding the lower bounds of each interval and the number of samples within each interval - if you want to display them, you'll need to use toList.



来源:https://stackoverflow.com/questions/48212509/how-can-i-compute-a-histogram-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!