Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.histogram

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

Compute the histogram of a set of data.

Parameters:
a:array_like

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

bins:int 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 the bin edges, including the rightmostedge, allowing for non-uniform bin widths.

New in version 1.11.0.

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.

normed:bool, optional

Deprecated since version 1.6.0.

This is equivalent to thedensity argument, but produces incorrectresults for unequal bin widths. It should not be used.

Changed in version 1.15.0:DeprecationWarnings are actually emitted.

weights:array_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.

density:bool, 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.

Overrides thenormed keyword if given.

Returns:
hist:array

The values of the histogram. Seedensity andweights for adescription of the possible semantics.

bin_edges:array 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

>>>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

New in version 1.11.0.

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

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

Previous topic

numpy.cov

Next topic

numpy.histogram2d

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp