Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.digitize

numpy.digitize(x,bins,right=False)

Return the indices of the bins to which each value in input array belongs.

rightorder of binsreturned indexi satisfies
Falseincreasingbins[i-1]<=x<bins[i]
Trueincreasingbins[i-1]<x<=bins[i]
Falsedecreasingbins[i-1]>x>=bins[i]
Truedecreasingbins[i-1]>=x>bins[i]

If values inx are beyond the bounds ofbins, 0 orlen(bins) isreturned as appropriate.

Parameters:
x:array_like

Input array to be binned. Prior to NumPy 1.10.0, this array had tobe 1-dimensional, but can now have any shape.

bins:array_like

Array of bins. It has to be 1-dimensional and monotonic.

right:bool, optional

Indicating whether the intervals include the right or the left binedge. Default behavior is (right==False) indicating that the intervaldoes not include the right edge. The left bin end is open in thiscase, i.e., bins[i-1] <= x < bins[i] is the default behavior formonotonically increasing bins.

Returns:
indices:ndarray of ints

Output array of indices, of same shape asx.

Raises:
ValueError

Ifbins is not monotonic.

TypeError

If the type of the input is complex.

Notes

If values inx are such that they fall outside the bin range,attempting to indexbins with the indices thatdigitize returnswill result in an IndexError.

New in version 1.10.0.

np.digitize is implemented in terms ofnp.searchsorted. This meansthat a binary search is used to bin the values, which scales much betterfor larger number of bins than the previous linear search. It also removesthe requirement for the input array to be 1-dimensional.

For monotonically _increasing_bins, the following are equivalent:

np.digitize(x,bins,right=True)np.searchsorted(bins,x,side='left')

Note that as the order of the arguments are reversed, the side must be too.Thesearchsorted call is marginally faster, as it does not do anymonotonicity checks. Perhaps more importantly, it supports all dtypes.

Examples

>>>x=np.array([0.2,6.4,3.0,1.6])>>>bins=np.array([0.0,1.0,2.5,4.0,10.0])>>>inds=np.digitize(x,bins)>>>indsarray([1, 4, 3, 2])>>>forninrange(x.size):...print(bins[inds[n]-1],"<=",x[n],"<",bins[inds[n]])...0.0 <= 0.2 < 1.04.0 <= 6.4 < 10.02.5 <= 3.0 < 4.01.0 <= 1.6 < 2.5
>>>x=np.array([1.2,10.0,12.4,15.5,20.])>>>bins=np.array([0,5,10,15,20])>>>np.digitize(x,bins,right=True)array([1, 2, 3, 4, 4])>>>np.digitize(x,bins,right=False)array([1, 3, 3, 4, 5])

Previous topic

numpy.histogram_bin_edges

Next topic

Test Support (numpy.testing)

Quick search

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

[8]ページ先頭

©2009-2025 Movatter.jp