问题
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 Vector
s 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