summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2018-06-13 01:12:55 -0700
committerEric Wieser <wieser.eric@gmail.com>2018-06-15 21:02:39 -0700
commit494e96a87fb6e5a4789354482e95a6a1d10cb23f (patch)
tree3165a452e1f7bf02ba399c4664915240795816e0
parent3d9a082d146195944071a81ed0eae9d16976961a (diff)
downloadnumpy-494e96a87fb6e5a4789354482e95a6a1d10cb23f.tar.gz
DEP: Actually deprecate the normed argument to histogram
Documenting this argument at such length gives it authenticity it does not deserve.
-rw-r--r--doc/release/1.15.0-notes.rst3
-rw-r--r--numpy/lib/histograms.py42
-rw-r--r--numpy/lib/tests/test_histograms.py46
3 files changed, 62 insertions, 29 deletions
diff --git a/doc/release/1.15.0-notes.rst b/doc/release/1.15.0-notes.rst
index cc193530c..1f3bf9bf3 100644
--- a/doc/release/1.15.0-notes.rst
+++ b/doc/release/1.15.0-notes.rst
@@ -77,6 +77,9 @@ Deprecations
`RuntimeWarning` will be emitted otherwise in these cases. Users of the C-API
should call ``NpyIter_Close`` before ``NpyIter_Deallocate``.
+* The ``normed`` argument of ``np.histogram``, deprecated long ago in 1.6.0,
+ now emits a ``DeprecationWarning``.
+
Future Changes
==============
diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py
index 2922b3a86..b7d9c4406 100644
--- a/numpy/lib/histograms.py
+++ b/numpy/lib/histograms.py
@@ -4,6 +4,7 @@ Histogram-related functions
from __future__ import division, absolute_import, print_function
import operator
+import warnings
import numpy as np
from numpy.compat.py3k import basestring
@@ -559,7 +560,7 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None):
return bin_edges
-def histogram(a, bins=10, range=None, normed=False, weights=None,
+def histogram(a, bins=10, range=None, normed=None, weights=None,
density=None):
r"""
Compute the histogram of a set of data.
@@ -591,14 +592,12 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
.. deprecated:: 1.6.0
- This keyword is deprecated in NumPy 1.6.0 due to confusing/buggy
- behavior. It will be removed in NumPy 2.0.0. Use the ``density``
- keyword instead. If ``False``, the result will contain the
- number of samples in each bin. If ``True``, 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 this latter behavior is known to be buggy with unequal bin
- widths; use ``density`` instead.
+ This is equivalent to the `density` argument, but produces incorrect
+ results for unequal bin widths. It should not be used.
+
+ .. versionchanged:: 1.15.0
+ DeprecationWarnings are actually emitted.
+
weights : array_like, optional
An array of weights, of the same shape as `a`. Each value in
`a` only contributes its associated weight towards the bin count
@@ -777,16 +776,39 @@ def histogram(a, bins=10, range=None, normed=False, weights=None,
# density overrides the normed keyword
if density is not None:
+ if normed is not None:
+ # 2018-06-13, numpy 1.15.0 (this was not noisily deprecated in 1.6)
+ warnings.warn(
+ "The normed argument is ignored when density is provided. "
+ "In future passing both will result in an error.",
+ DeprecationWarning, stacklevel=2)
normed = False
if density:
db = np.array(np.diff(bin_edges), float)
return n/db/n.sum(), bin_edges
elif normed:
- # deprecated, buggy behavior. Remove for NumPy 2.0.0
+ # 2018-06-13, numpy 1.15.0 (this was not noisily deprecated in 1.6)
+ warnings.warn(
+ "Passing `normed=True` on non-uniform bins has always been "
+ "broken, and computes neither the probability density "
+ "function nor the probability mass function. "
+ "The result is only correct if the bins are uniform, when "
+ "density=True will produce the same result anyway. "
+ "The argument will be removed in a future version of "
+ "numpy.",
+ np.VisibleDeprecationWarning, stacklevel=2)
+
+ # this normalization is incorrect, but
db = np.array(np.diff(bin_edges), float)
return n/(n*db).sum(), bin_edges
else:
+ if normed is not None:
+ # 2018-06-13, numpy 1.15.0 (this was not noisily deprecated in 1.6)
+ warnings.warn(
+ "Passing normed=False is deprecated, and has no effect. "
+ "Consider passing the density argument instead.",
+ DeprecationWarning, stacklevel=2)
return n, bin_edges
diff --git a/numpy/lib/tests/test_histograms.py b/numpy/lib/tests/test_histograms.py
index e16ae12c2..adaa7e3bd 100644
--- a/numpy/lib/tests/test_histograms.py
+++ b/numpy/lib/tests/test_histograms.py
@@ -40,20 +40,28 @@ class TestHistogram(object):
assert_allclose(e, np.array([1., 2.]))
def test_normed(self):
- # Check that the integral of the density equals 1.
- n = 100
- v = np.random.rand(n)
- a, b = histogram(v, normed=True)
- area = np.sum(a * np.diff(b))
- assert_almost_equal(area, 1)
+ sup = suppress_warnings()
+ with sup:
+ rec = sup.record(np.VisibleDeprecationWarning, '.*normed.*')
+ # Check that the integral of the density equals 1.
+ n = 100
+ v = np.random.rand(n)
+ a, b = histogram(v, normed=True)
+ area = np.sum(a * np.diff(b))
+ assert_almost_equal(area, 1)
+ assert_equal(len(rec), 1)
- # Check with non-constant bin widths (buggy but backwards
- # compatible)
- v = np.arange(10)
- bins = [0, 1, 5, 9, 10]
- a, b = histogram(v, bins, normed=True)
- area = np.sum(a * np.diff(b))
- assert_almost_equal(area, 1)
+ sup = suppress_warnings()
+ with sup:
+ rec = sup.record(np.VisibleDeprecationWarning, '.*normed.*')
+ # Check with non-constant bin widths (buggy but backwards
+ # compatible)
+ v = np.arange(10)
+ bins = [0, 1, 5, 9, 10]
+ a, b = histogram(v, bins, normed=True)
+ area = np.sum(a * np.diff(b))
+ assert_almost_equal(area, 1)
+ assert_equal(len(rec), 1)
def test_density(self):
# Check that the integral of the density equals 1.
@@ -96,12 +104,12 @@ class TestHistogram(object):
assert_equal(h.sum(), 9)
# Normalization
- h, b = histogram(a, range=[1, 9], normed=True)
+ h, b = histogram(a, range=[1, 9], density=True)
assert_almost_equal((h * np.diff(b)).sum(), 1, decimal=15)
# Weights
w = np.arange(10) + .5
- h, b = histogram(a, range=[1, 9], weights=w, normed=True)
+ h, b = histogram(a, range=[1, 9], weights=w, density=True)
assert_equal((h * np.diff(b)).sum(), 1)
h, b = histogram(a, bins=8, range=[1, 9], weights=w)
@@ -113,7 +121,7 @@ class TestHistogram(object):
h, b = histogram(a)
assert_(np.issubdtype(h.dtype, np.integer))
- h, b = histogram(a, normed=True)
+ h, b = histogram(a, density=True)
assert_(np.issubdtype(h.dtype, np.floating))
h, b = histogram(a, weights=np.ones(10, int))
@@ -133,9 +141,9 @@ class TestHistogram(object):
v = np.random.rand(100)
w = np.ones(100) * 5
a, b = histogram(v)
- na, nb = histogram(v, normed=True)
+ na, nb = histogram(v, density=True)
wa, wb = histogram(v, weights=w)
- nwa, nwb = histogram(v, weights=w, normed=True)
+ nwa, nwb = histogram(v, weights=w, density=True)
assert_array_almost_equal(a * 5, wa)
assert_array_almost_equal(na, nwa)
@@ -149,7 +157,7 @@ class TestHistogram(object):
wa, wb = histogram([1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1])
assert_array_equal(wa, [4, 5, 0, 1])
wa, wb = histogram(
- [1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], normed=True)
+ [1, 2, 2, 4], bins=4, weights=[4, 3, 2, 1], density=True)
assert_array_almost_equal(wa, np.array([4, 5, 0, 1]) / 10. / 3. * 4)
# Check weights with non-uniform bin widths