numpy.histogram#

numpy.histogram(a,bins=10,range=None,density=None,weights=None)[source]#

Compute the histogram of a dataset.

Parameters:
aarray_like

Input data. The histogram is computed over the flattened array.

binsint or sequence of scalars or str, optional

Ifbins is an int, it defines the number of equal-widthbins in the given range (10, by default). Ifbins is asequence, it defines a monotonically increasing array of bin edges,including the rightmost edge, allowing for non-uniform bin widths.

Ifbins is a string, it defines the method used to calculate theoptimal bin width, as defined byhistogram_bin_edges.

range(float, float), optional

The lower and upper range of the bins. If not provided, rangeis simply(a.min(),a.max()). Values outside the range areignored. The first element of the range must be less than orequal to the second.range affects the automatic bincomputation as well. While bin width is computed to be optimalbased on the actual data withinrange, the bin count will fillthe entire range including portions containing no data.

weightsarray_like, optional

An array of weights, of the same shape asa. Each value ina only contributes its associated weight towards the bin count(instead of 1). Ifdensity is True, the weights arenormalized, so that the integral of the density over the rangeremains 1.Please note that thedtype ofweights will also become thedtype of the returned accumulator (hist), so it must belarge enough to hold accumulated values as well.

densitybool, optional

IfFalse, the result will contain the number of samples ineach bin. IfTrue, the result is the value of theprobabilitydensity function at the bin, normalized such thattheintegral over the range is 1. Note that the sum of thehistogram values will not be equal to 1 unless bins of unitywidth are chosen; it is not a probabilitymass function.

Returns:
histarray

The values of the histogram. Seedensity andweights for adescription of the possible semantics. Ifweights are given,hist.dtype will be taken fromweights.

bin_edgesarray of dtype float

Return the bin edges(length(hist)+1).

Notes

All but the last (righthand-most) bin is half-open. In other words,ifbins is:

[1,2,3,4]

then the first bin is[1,2) (including 1, but excluding 2) andthe second[2,3). The last bin, however, is[3,4], whichincludes 4.

Examples

>>>importnumpyasnp>>>np.histogram([1,2,1],bins=[0,1,2,3])(array([0, 2, 1]), array([0, 1, 2, 3]))>>>np.histogram(np.arange(4),bins=np.arange(5),density=True)(array([0.25, 0.25, 0.25, 0.25]), array([0, 1, 2, 3, 4]))>>>np.histogram([[1,2,1],[1,0,1]],bins=[0,1,2,3])(array([1, 4, 1]), array([0, 1, 2, 3]))
>>>a=np.arange(5)>>>hist,bin_edges=np.histogram(a,density=True)>>>histarray([0.5, 0. , 0.5, 0. , 0. , 0.5, 0. , 0.5, 0. , 0.5])>>>hist.sum()2.4999999999999996>>>np.sum(hist*np.diff(bin_edges))1.0

Automated Bin Selection Methods example, using 2 peak random datawith 2000 points.

importmatplotlib.pyplotaspltimportnumpyasnprng=np.random.RandomState(10)# deterministic random dataa=np.hstack((rng.normal(size=1000),rng.normal(loc=5,scale=2,size=1000)))plt.hist(a,bins='auto')# arguments are passed to np.histogramplt.title("Histogram with 'auto' bins")plt.show()
../../_images/numpy-histogram-1.png
On this page