numpy.histogram()如何工作? [英] How does numpy.histogram() work?

查看:130
本文介绍了numpy.histogram()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读numpy时,我遇到了 numpy.histogram()

While reading up on numpy, I encountered the function numpy.histogram().

它的作用是什么?它是如何工作的??在文档中,他们提到了 bins :它们是什么?

What is it for and how does it work? In the docs they mention bins: What are they?

有些谷歌搜索使我想到了直方图的一般定义.我明白了.但是很遗憾,我无法将这些知识与文档中给出的示例联系起来.

Some googling led me to the definition of Histograms in general. I get that. But unfortunately I can't link this knowledge to the examples given in the docs.

推荐答案

bin是一个范围,表示直方图的单个条形沿X轴的宽度.您也可以将其称为间隔. (维基百科更正式地将它们定义为不相交类别".)

A bin is range that represents the width of a single bar of the histogram along the X-axis. You could also call this the interval. (Wikipedia defines them more formally as "disjoint categories".)

Numpy histogram函数不会绘制直方图,但会计算落入每个仓中的输入数据的出现次数,这反过来会确定面积(如果仓的宽度不相等,则不一定是高度) ).

The Numpy histogram function doesn't draw the histogram, but it computes the occurrences of input data that fall within each bin, which in turns determines the area (not necessarily the height if the bins aren't of equal width) of each bar.

在此示例中:

 np.histogram([1, 2, 1], bins=[0, 1, 2, 3])

共有3个bin,其值分别从0到1(不包括1),1到2(不包括2)和2到3(包括3).在本示例中,如果Numpy通过提供定界符列表([0, 1, 2, 3])来定义这些bin,尽管它也会返回结果中的bin,因为如果未指定,则可以从输入中自动选择它们.例如,如果bins=5,它将在最小输入值和最大输入值之间使用5个等宽分布的bin.

There are 3 bins, for values ranging from 0 to 1 (excl 1.), 1 to 2 (excl. 2) and 2 to 3 (incl. 3), respectively. The way Numpy defines these bins if by giving a list of delimiters ([0, 1, 2, 3]) in this example, although it also returns the bins in the results, since it can choose them automatically from the input, if none are specified. If bins=5, for example, it will use 5 bins of equal width spread between the minimum input value and the maximum input value.

输入值为1、2和1.因此,仓位"1至2"包含两次出现(两个1值),仓位"2至3"包含一次出现(2).这些结果在返回的元组的第一项中:array([0, 2, 1]).

The input values are 1, 2 and 1. Therefore, bin "1 to 2" contains two occurrences (the two 1 values), and bin "2 to 3" contains one occurrence (the 2). These results are in the first item in the returned tuple: array([0, 2, 1]).

由于此处的垃圾箱宽度相等,因此您可以使用出现次数来表示每个条形的高度.绘制后,您将具有:

Since the bins here are of equal width, you can use the number of occurrences for the height of each bar. When drawn, you would have:

  • X轴上范围/箱[0,1]的高度为0的条形
  • 范围/档位[1,2]的高度为2的条,
  • 范围/箱[2,3]的高度为1的条.

您可以直接使用Matplotlib绘制此图(它的hist函数还返回垃圾箱和值):

You can plot this directly with Matplotlib (its hist function also returns the bins and the values):

>>> import matplotlib.pyplot as plt
>>> plt.hist([1, 2, 1], bins=[0, 1, 2, 3])
(array([0, 2, 1]), array([0, 1, 2, 3]), <a list of 3 Patch objects>)
>>> plt.show()

这篇关于numpy.histogram()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆