【问题标题】:How can I generate a complete histogram with numpy?如何使用 numpy 生成完整的直方图?
【发布时间】:2009-09-14 08:02:22
【问题描述】:

我在numpy.array 中有一个很长的列表。我想为它生成一个直方图。但是,Numpy 的 built in histogram 需要预定义数量的 bin。为每个值生成一个包含一个 bin 的完整直方图的最佳方法是什么?

【问题讨论】:

    标签: python numpy histogram


    【解决方案1】:

    如果你有一个整数数组并且最大值不是太大,你可以使用 numpy.bincount:

    hist = dict((key,val) for key, val in enumerate(numpy.bincount(data)) if val)
    

    编辑: 如果您有浮点数据,或者数据分布在很大范围内,您可以通过以下方式将其转换为整数:

    bins = numpy.unique(data)
    bincounts = numpy.bincount(numpy.digitize(data, bins) - 1)
    hist = dict(zip(bins, bincounts))
    

    【讨论】:

    • np.bincount(np.digitize(...)) 是一个非常有用的模式:)
    【解决方案2】:

    每个值的 bin 听起来有点奇怪,但不会

    bins=a.max()-a.min()
    

    给出类似的结果?

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2019-01-05
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2016-01-11
      • 2015-12-10
      相关资源
      最近更新 更多