Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a44b61c

Browse files
authored
Merge pull request #11529 from eric-wieser/histogramdd-density-no-deprecation
ENH: Add density argument to histogramdd.
2 parents 6c524f2 + 8ea9e8b commit a44b61c

File tree

5 files changed

+80
-16
lines changed

5 files changed

+80
-16
lines changed

doc/release/1.15.0-notes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@ The ``range`` argument of `numpy.histogramdd` can now contain ``None`` values to
329329
indicate that the range for the corresponding axis should be computed from the
330330
data. Previously, this could not be specified on a per-axis basis.
331331

332+
The normed arguments of ``histogramdd`` and ``histogram2d`` have been renamed
333+
-----------------------------------------------------------------------------
334+
These arguments are now called ``density``, which is consistent with
335+
``histogram``. The old argument continues to work, but the new name should be
336+
preferred.
337+
332338
``np.r_`` works with 0d arrays, and ``np.ma.mr_`` works with ``np.ma.masked``
333339
-----------------------------------------------------------------------------
334340
0d arrays passed to the `r_` and `mr_` concatenation helpers are now treated as

numpy/lib/histograms.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,8 @@ def histogram(a, bins=10, range=None, normed=None, weights=None,
812812
return n, bin_edges
813813

814814

815-
def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
815+
def histogramdd(sample, bins=10, range=None, normed=None, weights=None,
816+
density=None):
816817
"""
817818
Compute the multidimensional histogram of some data.
818819
@@ -844,9 +845,14 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
844845
An entry of None in the sequence results in the minimum and maximum
845846
values being used for the corresponding dimension.
846847
The default, None, is equivalent to passing a tuple of D None values.
848+
density : bool, optional
849+
If False, the default, returns the number of samples in each bin.
850+
If True, returns the probability *density* function at the bin,
851+
``bin_count / sample_count / bin_volume``.
847852
normed : bool, optional
848-
If False, returns the number of samples in each bin. If True,
849-
returns the bin density ``bin_count / sample_count / bin_volume``.
853+
An alias for the density argument that behaves identically. To avoid
854+
confusion with the broken normed argument to `histogram`, `density`
855+
should be preferred.
850856
weights : (N,) array_like, optional
851857
An array of values `w_i` weighing each sample `(x_i, y_i, z_i, ...)`.
852858
Weights are normalized to 1 if normed is True. If normed is False,
@@ -960,8 +966,18 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
960966
core = D*(slice(1, -1),)
961967
hist = hist[core]
962968

963-
# Normalize if normed is True
964-
if normed:
969+
# handle the aliasing normed argument
970+
if normed is None:
971+
if density is None:
972+
density = False
973+
elif density is None:
974+
# an explicit normed argument was passed, alias it to the new name
975+
density = normed
976+
else:
977+
raise TypeError("Cannot specify both 'normed' and 'density'")
978+
979+
if density:
980+
# calculate the probability density function
965981
s = hist.sum()
966982
for i in _range(D):
967983
shape = np.ones(D, int)

numpy/lib/tests/test_histograms.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,13 @@ def test_simple(self):
547547

548548
# Check normalization
549549
ed = [[-2, 0, 2], [0, 1, 2, 3], [0, 1, 2, 3]]
550-
H, edges = histogramdd(x, bins=ed, normed=True)
550+
H, edges = histogramdd(x, bins=ed, density=True)
551551
assert_(np.all(H == answer / 12.))
552552

553553
# Check that H has the correct shape.
554554
H, edges = histogramdd(x, (2, 3, 4),
555555
range=[[-1, 1], [0, 3], [0, 4]],
556-
normed=True)
556+
density=True)
557557
answer = np.array([[[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]],
558558
[[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]]])
559559
assert_array_almost_equal(H, answer / 6., 4)
@@ -599,10 +599,10 @@ def test_shape_4d(self):
599599
def test_weights(self):
600600
v = np.random.rand(100, 2)
601601
hist, edges = histogramdd(v)
602-
n_hist, edges = histogramdd(v, normed=True)
602+
n_hist, edges = histogramdd(v, density=True)
603603
w_hist, edges = histogramdd(v, weights=np.ones(100))
604604
assert_array_equal(w_hist, hist)
605-
w_hist, edges = histogramdd(v, weights=np.ones(100) * 2, normed=True)
605+
w_hist, edges = histogramdd(v, weights=np.ones(100) * 2, density=True)
606606
assert_array_equal(w_hist, n_hist)
607607
w_hist, edges = histogramdd(v, weights=np.ones(100, int) * 2)
608608
assert_array_equal(w_hist, 2 * hist)
@@ -707,3 +707,39 @@ def test_large_integers(self):
707707
hist, edges = histogramdd((x, y), bins=(x_edges, y_edges))
708708

709709
assert_equal(hist[0, 0], 1)
710+
711+
def test_density_non_uniform_2d(self):
712+
# Defines the following grid:
713+
#
714+
# 0 2 8
715+
# 0+-+-----+
716+
# + | +
717+
# + | +
718+
# 6+-+-----+
719+
# 8+-+-----+
720+
x_edges = np.array([0, 2, 8])
721+
y_edges = np.array([0, 6, 8])
722+
relative_areas = np.array([
723+
[3, 9],
724+
[1, 3]])
725+
726+
# ensure the number of points in each region is proportional to its area
727+
x = np.array([1] + [1]*3 + [7]*3 + [7]*9)
728+
y = np.array([7] + [1]*3 + [7]*3 + [1]*9)
729+
730+
# sanity check that the above worked as intended
731+
hist, edges = histogramdd((y, x), bins=(y_edges, x_edges))
732+
assert_equal(hist, relative_areas)
733+
734+
# resulting histogram should be uniform, since counts and areas are propotional
735+
hist, edges = histogramdd((y, x), bins=(y_edges, x_edges), density=True)
736+
assert_equal(hist, 1 / (8*8))
737+
738+
def test_density_non_uniform_1d(self):
739+
# compare to histogram to show the results are the same
740+
v = np.arange(10)
741+
bins = np.array([0, 1, 3, 6, 10])
742+
hist, edges = histogram(v, bins, density=True)
743+
hist_dd, edges_dd = histogramdd((v,), (bins,), density=True)
744+
assert_equal(hist, hist_dd)
745+
assert_equal(edges, edges_dd[0])

numpy/lib/tests/test_twodim_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def test_asym(self):
208208
x = array([1, 1, 2, 3, 4, 4, 4, 5])
209209
y = array([1, 3, 2, 0, 1, 2, 3, 4])
210210
H, xed, yed = histogram2d(
211-
x, y, (6, 5), range=[[0, 6], [0, 5]], normed=True)
211+
x, y, (6, 5), range=[[0, 6], [0, 5]], density=True)
212212
answer = array(
213213
[[0., 0, 0, 0, 0],
214214
[0, 1, 0, 1, 0],
@@ -220,11 +220,11 @@ def test_asym(self):
220220
assert_array_equal(xed, np.linspace(0, 6, 7))
221221
assert_array_equal(yed, np.linspace(0, 5, 6))
222222

223-
def test_norm(self):
223+
def test_density(self):
224224
x = array([1, 2, 3, 1, 2, 3, 1, 2, 3])
225225
y = array([1, 1, 1, 2, 2, 2, 3, 3, 3])
226226
H, xed, yed = histogram2d(
227-
x, y, [[1, 2, 3, 5], [1, 2, 3, 5]], normed=True)
227+
x, y, [[1, 2, 3, 5], [1, 2, 3, 5]], density=True)
228228
answer = array([[1, 1, .5],
229229
[1, 1, .5],
230230
[.5, .5, .25]])/9.

numpy/lib/twodim_base.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@ def vander(x, N=None, increasing=False):
530530
return v
531531

532532

533-
def histogram2d(x, y, bins=10, range=None, normed=False, weights=None):
533+
def histogram2d(x, y, bins=10, range=None, normed=None, weights=None,
534+
density=None):
534535
"""
535536
Compute the bi-dimensional histogram of two data samples.
536537
@@ -560,9 +561,14 @@ def histogram2d(x, y, bins=10, range=None, normed=False, weights=None):
560561
(if not specified explicitly in the `bins` parameters):
561562
``[[xmin, xmax], [ymin, ymax]]``. All values outside of this range
562563
will be considered outliers and not tallied in the histogram.
564+
density : bool, optional
565+
If False, the default, returns the number of samples in each bin.
566+
If True, returns the probability *density* function at the bin,
567+
``bin_count / sample_count / bin_area``.
563568
normed : bool, optional
564-
If False, returns the number of samples in each bin. If True,
565-
returns the bin density ``bin_count / sample_count / bin_area``.
569+
An alias for the density argument that behaves identically. To avoid
570+
confusion with the broken normed argument to `histogram`, `density`
571+
should be preferred.
566572
weights : array_like, shape(N,), optional
567573
An array of values ``w_i`` weighing each sample ``(x_i, y_i)``.
568574
Weights are normalized to 1 if `normed` is True. If `normed` is
@@ -652,7 +658,7 @@ def histogram2d(x, y, bins=10, range=None, normed=False, weights=None):
652658
if N != 1 and N != 2:
653659
xedges = yedges = asarray(bins)
654660
bins = [xedges, yedges]
655-
hist, edges = histogramdd([x, y], bins, range, normed, weights)
661+
hist, edges = histogramdd([x, y], bins, range, normed, weights, density)
656662
return hist, edges[0], edges[1]
657663

658664

0 commit comments

Comments
 (0)