如何在Haskell中计算直方图? [英] How can I compute a histogram in Haskell?

查看:53
本文介绍了如何在Haskell中计算直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了 Statistics.Sample.Histogram ,但是我似乎无法使用它.如果我希望将列表分为四个类别,则希望能够执行以下操作:

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?

推荐答案

fromList 函数将您的列表转换为 Vector :

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

结果是一对 Vector ,它们分别包含每个间隔的下限和每个间隔内的样本数-如果要显示它们,则需要使用 toList .

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.

这篇关于如何在Haskell中计算直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆