8000 BUG: Histogram value overflow when dtype of weights is integer of smaller size · Issue #23681 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG: Histogram value overflow when dtype of weights is integer of smaller size #23681
@AlkaidCheng

Description

@AlkaidCheng

Describe the issue:

When passing weights to numpy.histogram, if the dtype of weights has a size smaller than the output histogram value, an overflow will happen which gives a gibberish result. A possible solution is to automatically expand the dtype of the output histogram to the appropriate size.

Reproduce the code example:

import numpy as np
size = 100000
np.random.seed(0)
x = np.random.rand(size)
weights = np.ones(size, dtype='int8')
hist, bin_edges = np.histogram(x, weights=weights, range=(0, 1), bins=20)

# this gives hist = 
# array([ 100,    5,  -28,  122,  -89,   51, -114,    0,  -92,  -76,  127,
#         -8,   29,   64,  -96,   92,  -22,   54,  -78,  119], dtype=int8)

Error message:

No response

Runtime information:

Numpy 1.23.3

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0