10000 ENH: #7845. histogram2d and histogramdd can return int array when relevant by tom-bird · Pull Request #7886 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: #7845. histogram2d and histogramdd can return int array when relevant #7886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
ENH: #7845. histogram2d and histogramdd return int array when relevan…
…t, in line with histogram
  • Loading branch information
tom-bird committed Aug 1, 2016
commit 40d7d526520d9df19d696c32b44f3c70330c6533
7 changes: 6 additions & 1 deletion numpy/lib/function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,11 +878,16 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
N, D = sample.shape

nbin = empty(D, int)
ntype = np.dtype(np.intp)
edges = D*[None]
dedges = D*[None]
if weights is not None:
ntype = weights.dtype
weights = asarray(weights)

if normed:
ntype = np.dtype(np.float64)

try:
M = len(bins)
if M != D:
Expand Down Expand Up @@ -970,7 +975,7 @@ def histogramdd(sample, bins=10, range=None, normed=False, weights=None):
# Flattened histogram matrix (1D)
# Reshape is used so that overlarge arrays
# will raise an error.
hist = zeros(nbin, float).reshape(-1)
hist = zeros(nbin, ntype).reshape(-1)

# Compute the sample indices in the flattened histogram matrix.
ni = nbin.argsort()
Expand Down
10 changes: 10 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,16 @@ def test_finite_range(self):
assert_raises(ValueError, histogramdd, vals,
range=[[0.0, 1.0], [np.nan, 0.75], [0.25, 0.5]])

def test_type(self):
vals = np.random.random((10, 3))
w = np.ones(10, float)
assert_(np.issubdtype(np.histogramdd(vals)[0].dtype, int))
assert_(np.issubdtype(np.histogramdd(vals, weights=w)[0].dtype, float))
w = np.ones(10, int)
assert_(np.issubdtype(np.histogramdd(vals, weights=w)[0].dtype, int))
assert_(np.issubdtype(np.histogramdd(vals, normed=True)[0].dtype, float))
assert_(np.issubdtype(np.histogramdd(vals, weights=w, normed=True)[0].dtype, float))


class TestUnique(TestCase):

Expand Down
0