diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-12-27 14:35:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-27 14:35:23 -0700 |
commit | 6c4b09259c04630d7bfaceb3ae7c67c9ea2e7386 (patch) | |
tree | ecd72bc5640a947572fbd98f41a0fedbfa7e3fe0 /numpy/lib/histograms.py | |
parent | a10bf46fe7186e0555fbff55dcef4cd3291724a3 (diff) | |
parent | 0953c4d9543bd5e3c007f1013cf93779314ca327 (diff) | |
download | numpy-6c4b09259c04630d7bfaceb3ae7c67c9ea2e7386.tar.gz |
Merge pull request #10282 from eric-wieser/_search_sorted_histogram
MAINT: Extract helper function for last-bound-inclusive search_sorted
Diffstat (limited to 'numpy/lib/histograms.py')
-rw-r--r-- | numpy/lib/histograms.py | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index a19ff07d8..ec2d0fe81 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -326,6 +326,18 @@ def _get_bin_edges(a, bins, range, weights): return bin_edges, None +def _search_sorted_inclusive(a, v): + """ + Like `searchsorted`, but where the last item in `v` is placed on the right. + + In the context of a histogram, this makes the last bin edge inclusive + """ + return np.concatenate(( + a.searchsorted(v[:-1], 'left'), + a.searchsorted(v[-1:], 'right') + )) + + def histogram(a, bins=10, range=None, normed=False, weights=None, density=None): r""" @@ -626,19 +638,17 @@ def histogram(a, bins=10, range=None, normed=False, weights=None, if weights is None: for i in np.arange(0, len(a), BLOCK): sa = np.sort(a[i:i+BLOCK]) - cum_n += np.r_[sa.searchsorted(bin_edges[:-1], 'left'), - sa.searchsorted(bin_edges[-1], 'right')] + cum_n += _search_sorted_inclusive(sa, bin_edges) else: - zero = np.array(0, dtype=ntype) + zero = np.zeros(1, dtype=ntype) for i in np.arange(0, len(a), BLOCK): tmp_a = a[i:i+BLOCK] tmp_w = weights[i:i+BLOCK] sorting_index = np.argsort(tmp_a) sa = tmp_a[sorting_index] sw = tmp_w[sorting_index] - cw = np.concatenate(([zero], sw.cumsum())) - bin_index = np.r_[sa.searchsorted(bin_edges[:-1], 'left'), - sa.searchsorted(bin_edges[-1], 'right')] + cw = np.concatenate((zero, sw.cumsum())) + bin_index = _search_sorted_inclusive(sa, bin_edges) cum_n += cw[bin_index] n = np.diff(cum_n) |