numpy的histogram2d()函数详解

周作人
2023-12-01

这个函数可以做什么?

这个函数可以把两个二维数组,做出它的直方图出来。
什么叫做直方图:比如两个数组(A,B)都是5x5大小的,两个数组的元素都只有三个数字0,1,2
这时候得到的一个数组就是3x3的数组
(((0,0)的数量,(0,1)的数量,(0,2)的数量),
((1,0)的数量,(1,1)的数量,(1,2)的数量),
((2,0)的数量,(2,1)的数量,(2,2)的数量))
解释一下最里面这个(0,0)之类的
(A的位置是0,B在相同位置也是0)

后面跟着的两个array分别是直方图X维度和Y维度的刻度。

def histogram2d(x, y, bins=10, range=None, normed=False, weights=None):

其他参数自己看就可以了

Parameters
----------
x : array_like, shape (N,)
    An array containing the x coordinates of the points to be
    histogrammed.
y : array_like, shape (N,)
    An array containing the y coordinates of the points to be
    histogrammed.
bins : int or array_like or [int, int] or [array, array], optional
    The bin specification:

      * If int, the number of bins for the two dimensions (nx=ny=bins).
      * If array_like, the bin edges for the two dimensions
        (x_edges=y_edges=bins).
      * If [int, int], the number of bins in each dimension
        (nx, ny = bins).
      * If [array, array], the bin edges in each dimension
        (x_edges, y_edges = bins).
      * A combination [int, array] or [array, int], where int
        is the number of bins and array is the bin edges.

range : array_like, shape(2,2), optional
    The leftmost and rightmost edges of the bins along each dimension
    (if not specified explicitly in the `bins` parameters):
    ``[[xmin, xmax], [ymin, ymax]]``. All values outside of this range
    will be considered outliers and not tallied in the histogram.
normed : bool, optional
    If False, returns the number of samples in each bin. If True,
    returns the bin density ``bin_count / sample_count / bin_area``.
weights : array_like, shape(N,), optional
    An array of values ``w_i`` weighing each sample ``(x_i, y_i)``.
    Weights are normalized to 1 if `normed` is True. If `normed` is
    False, the values of the returned histogram are equal to the sum of
    the weights belonging to the samples falling into each bin.

Returns
-------
H : ndarray, shape(nx, ny)
    The bi-dimensional histogram of samples `x` and `y`. Values in `x`
    are histogrammed along the first dimension and values in `y` are
    histogrammed along the second dimension.
xedges : ndarray, shape(nx+1,)
    The bin edges along the first dimension.
yedges : ndarray, shape(ny+1,)
    The bin edges along the second dimension.

实例代码
两个数组(A,B)都是5x5大小的,两个数组的元素都只有三个数字0,1,2
获得其直方图的结果

#简单来说就是x,y为两个(N,)形式的数组,bin可以接受int或者数组或者列表
#一般来说就是一个(int,int)的元组,这样也好判定,这时候就直接看一下有多少个元素全部加进去算出来就可以了
#
from skimage.morphology import label
import numpy as np
x = np.zeros((5,5))
y = np.zeros((5,5))
x[0,0] = 1
x[0,1] = 1
x[0,2] = 1
x[3,2] = 1
x[3,3] = 1
x[3,1] = 1
y[0,0] = 1
y[0,1] = 1
y[4,0] = 1
y[4,1] = 1

print(x)
print(y)
labels = label(x)
y_pred = label(y)
#如果不连通就会加上新的标签,例如2,把1改成2
print("labels:\n")
print(labels,'\n',y_pred)
# unique 就是确定的意思,这里只有0,1,2所以下面计算的结果都是3
true_objects = len(np.unique(labels))
pred_objects = len(np.unique(y_pred))
print(true_objects,pred_objects)
#默认是10刻度的,但是我们只有0,1,2三个刻度,这时候如果把他们分成10个刻度就是(0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2.)但是如果我们分成三个刻度,(就是0,0.666,1.3333,2)这样就能把0,1,2区分开了
intersection = np.histogram2d(labels.flatten(), y_pred.flatten(),bins=(true_objects, pred_objects))
print(intersection)

如果以前没有学过计算机上的直方图概念的可能会感觉有点迷惑。但是直方图的目的其实就是看两个数组里面相同位置上(A的[1,2]位置上是0,B的[1,2]位置上是1),那么(0,1)这样的组合有多少个呢?
这就是直方图统计的内容啦!
是不是非常的简单易懂呢!

 类似资料: