当前位置:网站首页>numpy的histogram2d()函数详解
numpy的histogram2d()函数详解
2022-07-19 18:14:00 【Cierlly】
这个函数可以做什么?
这个函数可以把两个二维数组,做出它的直方图出来。
什么叫做直方图:比如两个数组(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)这样的组合有多少个呢?
这就是直方图统计的内容啦!
是不是非常的简单易懂呢!
边栏推荐
- The difference between if, ifdef and ifndef
- Pygame 官方文档 【翻译】- pygame.freetype
- Leetcode daily practice - 17.04 Vanishing numbers
- 【软件测试】—— 测试分类
- Basset: learning the regulatory code of the accessible genome with deep convolutional neural network
- 神器在手随时随地获取最新技术前沿
- SIGGRAPH 2022--岩鸽彩虹色羽毛渲染阅读笔记:Rendering Iridescent Rock Dove Neck Feathers
- Bug---redis反序列化失败
- [software test] - water cup test case
- 关于君正T41、T40、T31版本的选择参考都在这里
猜你喜欢
随机推荐
GPS、基站、IP定位的区别及其应用方向
适配动态 | 6月份SequoiaDB与5款产品完成互认证
Postgresql学习04-pg_hint_plan安装及使用、Sql优化小知识
ARM PWN基础教程
云服务器ECS老用户专享,10余款实例新购低至3.6折
30亿美元!地平线成全球估值最高AI芯片独角兽
TCL展示多款折叠屏手机:屏幕、铰链均为自主研发!
ES流、PES流、PS流和TS流介绍
【超详细图解】FPN + Mask RCNN
数据单位---bit 与 byte
赛迪发布《湖仓一体技术研究报告》,巨杉数据库入选国内企业典型代表
Structure assignment in ES6
Promise实现
【软件测试】—— 水杯测试用例
定价1.73万!华为Mate X凭什么正面硬罡三星Fold?
一文看懂深圳半导体产业现状与未来发展机遇
展锐首款5G基带芯片正式发布,成功跻身5G第一梯队!
Artifact in hand anytime, anywhere access to the latest technological frontier
R语言游程检验:使用runs.test函数对二值序列数据执行游程检验(检验序列是否是随机的)
Differences between IPv4 and IPv6