maxframe.tensor.histogram#

maxframe.tensor.histogram(a, bins=10, range=None, weights=None, density=None)[源代码]#

计算一组数据的直方图。

参数:
  • a (array_like) -- 输入数据。直方图是在展平的 tensor 上计算的。

  • bins (int or sequence of scalars or str, optional) -- 如果 bins 是一个整数,则定义了给定范围内的等宽 bins 数量(默认为 10)。如果 bins 是一个序列,则定义了一个单调递增的 bin 边缘张量,包括最右边的边缘,允许非均匀的 bin 宽度。如果 bins 是一个字符串,则定义了由 histogram_bin_edges 定义的计算最佳 bin 宽度的方法。

  • range ((float, float), optional) -- bins 的下限和上限。如果未提供,则范围为 (a.min(), a.max())。范围外的值将被忽略。范围的第一个元素必须小于或等于第二个元素。range 也会影响自动 bin 计算。虽然 bin 宽度是根据 range 内的实际数据计算出最优值,但 bin 计数将填充整个范围,包括不包含数据的部分。

  • weights (array_like, optional) -- 一个权重张量,形状与 a 相同。a 中的每个值仅将其关联的权重贡献给 bin 计数(而不是 1)。如果 density 为 True,则对权重进行归一化,使得密度在范围内的积分为 1。

  • density (bool, optional) -- 如果为 False,结果将包含每个 bin 中的样本数量。如果为 True,结果是 bin 处的概率 密度 函数值,归一化为在该范围内的 积分 为 1。注意,只有在选择了单位宽度的 bins 时,直方图值的总和才会等于 1;它不是概率 质量 函数。如果给出,将覆盖 normed 关键字。

返回:

  • hist (tensor) -- 直方图的值。请参见 densityweights 以了解可能的语义描述。

  • bin_edges (dtype 为 float 的 tensor) -- 返回 bin 边缘 (length(hist)+1)

参见

histogramdd, bincount, searchsorted, digitize, histogram_bin_edges

备注

除最后一个(最右边的)bin 之外的所有 bin 都是半开的。换句话说,如果 bins 是::

[1, 2, 3, 4]

则第一个 bin 是 [1, 2)``(包含 1,但不包含 2),第二个是 ``[2, 3)。然而,最后一个 bin 是 [3, 4],它 包含 4。

示例

>>> import maxframe.tensor as mt
>>> mt.histogram([1, 2, 1], bins=[0, 1, 2, 3]).execute()
(array([0, 2, 1]), array([0, 1, 2, 3]))
>>> mt.histogram(mt.arange(4), bins=mt.arange(5), density=True).execute()
(array([0.25, 0.25, 0.25, 0.25]), array([0, 1, 2, 3, 4]))
>>> mt.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3]).execute()
(array([1, 4, 1]), array([0, 1, 2, 3]))
>>> a = mt.arange(5)
>>> hist, bin_edges = mt.histogram(a, density=True)
>>> hist.execute()
array([0.5, 0. , 0.5, 0. , 0. , 0.5, 0. , 0.5, 0. , 0.5])
>>> hist.sum().execute()
2.4999999999999996
>>> mt.sum(hist * mt.diff(bin_edges)).execute()
1.0

自动 Bin 选择方法示例,使用 2 个峰值的随机数据,共 2000 个点:

>>> import matplotlib.pyplot as plt
>>> rng = mt.random.RandomState(10)  # deterministic random data
>>> a = mt.hstack((rng.normal(size=1000),
...                rng.normal(loc=5, scale=2, size=1000)))
>>> _ = plt.hist(np.asarray(a), bins='auto')  # arguments are passed to np.histogram
>>> plt.title("Histogram with 'auto' bins")
Text(0.5, 1.0, "Histogram with 'auto' bins")
>>> plt.show()