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

查看:32
本文介绍了如何在 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?

推荐答案

histogram 需要一个 Vector 值,而不是一个列表.您可以使用 Data.VectorfromList 函数将您的列表转换为 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天全站免登陆