np.histogram2d() 实例详解

汲丰茂
2023-12-01

函数

以下是我从源码里copy来的,简单说下,x,y为两个(N,)形式的数组,bin可以接受int或者数组或者列表,下面的例子我们以列表为例,表示两个输入各自所分的刻度。结果返回一个数组,我们来具体看一下。
def histogram2d(x, y, bins=10, range=None, normed=False, weights=None):
“”"
Compute the bi-dimensional histogram of two data samples.

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.

实例

以下代码我们可以直接从np.histogram2d()来看,相当于输入两个(5,5)的数组,然后返回他们的二维直方图。结果为包含3个array的tuple,我们通常看第一个array,后两个array为细分的刻度情况,数组中最大值为2,都分三个刻度,就如后两个数组所示。
第一个数组对角线处表示两个输入对应位置相同的个数,例如两个输入对应位置都为0的个数为17,两个输入对应位置都为1的个数为2。而对角线上下两侧则表示两个输入的相同值不在同一位置的个数,例如1在两个输入中,有一个不在同一位置,有两个在同一位置。同理可以验证一下2。
11100 00000 00000 02220 00000
11000 00000 00000 00000 22000

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
labels = label(x)
y_pred = label(y)
print(labels,'\n',y_pred)
true_objects = len(np.unique(labels))
pred_objects = len(np.unique(y_pred))
print(true_objects,pred_objects)
intersection = np.histogram2d(labels.flatten(), y_pred.flatten(), bins=(true_objects, pred_objects))
print(intersection)
output:
[[1 1 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 2 2 2 0]
 [0 0 0 0 0]] 
 [[1 1 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]
 [2 2 0 0 0]]
3 3
(array([[17.,  0.,  2.],
       [ 1.,  2.,  0.],
       [ 3.,  0.,  0.]]), array([0.        , 0.66666667, 1.33333333, 2.        ]), array([0.        , 0.66666667, 1.33333333, 2.        ]))
 类似资料: