bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / histogram
_ArrayFunctionDispatcher
numpy:histogram
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/lib/_histograms_impl.py :685
Signature
def histogram ( a , bins = 10 , range = None , density = None , weights = None ) Summary
Compute the histogram of a dataset.
Parameters
a: array_likeInput data. The histogram is computed over the flattened array.
bins: int or sequence of scalars or str, optionalIf
binsis an int, it defines the number of equal-width bins in the given range (10, by default). Ifbinsis a sequence, it defines a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform bin widths.If
binsis a string, it defines the method used to calculate the optimal bin width, as defined by histogram_bin_edges.range: (float, float), optionalThe lower and upper range of the bins. If not provided, range is simply
(a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second.rangeaffects the automatic bin computation as well. While bin width is computed to be optimal based on the actual data withinrange, the bin count will fill the entire range including portions containing no data.weights: array_like, optionalAn array of weights, of the same shape as
a. Each value inaonly contributes its associated weight towards the bin count (instead of 1). Ifdensityis True, the weights are normalized, so that the integral of the density over the range remains 1. Please note that thedtypeofweightswill also become thedtypeof the returned accumulator (hist), so it must be large enough to hold accumulated values as well.density: bool, optionalIf
False, the result will contain the number of samples in each bin. IfTrue, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function.
Returns
hist: arrayThe values of the histogram. See
densityandweightsfor a description of the possible semantics. Ifweightsare given,hist.dtypewill be taken fromweights.bin_edges: array of dtype floatReturn the bin edges
(length(hist)+1).
Notes
All but the last (righthand-most) bin is half-open. In other words, if bins is
[1, 2, 3, 4]then the first bin is [1, 2) (including 1, but excluding 2) and the second [2, 3). The last bin, however, is [3, 4], which includes 4.
Examples
import numpy as np np.histogram([1, 2, 1], bins=[0, 1, 2, 3]) np.histogram(np.arange(4), bins=np.arange(5), density=True) np.histogram([[1, 2, 1], [1, 0, 1]], bins=[0,1,2,3])✓
a = np.arange(5) hist, bin_edges = np.histogram(a, density=True) hist✓
hist.sum() np.sum(hist * np.diff(bin_edges))✗
See also
- bincount
- digitize
- histogram_bin_edges
- histogramdd
- searchsorted
Aliases
-
numpy.histogram