Skip to content
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

[FIX] util: Fix bincount for object arrays #3831

Merged
merged 1 commit into from
Jun 2, 2019
Merged
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
6 changes: 3 additions & 3 deletions Orange/statistics/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def sparse_implicit_zero_weights(x, weights):
def bincount(x, weights=None, max_val=None, minlength=0):
"""Return counts of values in array X.

Works kind of like np.bincount(), except that it also supports floating
Works kind of like np.bincount(), except that it also supports
arrays with nans.

Parameters
Expand Down Expand Up @@ -132,8 +132,8 @@ def bincount(x, weights=None, max_val=None, minlength=0):

x = x.data

x = np.asanyarray(x)
if x.dtype.kind == 'f' and bn.anynan(x):
x = np.asanyarray(x, dtype=float)
if bn.anynan(x):
nonnan = ~np.isnan(x)
x = x[nonnan]
if weights is not None:
Expand Down
7 changes: 7 additions & 0 deletions Orange/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,13 @@ def test_count_nans(self, array):

np.testing.assert_equal(bincount(x)[1], expected)

# object arrays cannot be converted to sparse, so test only for dense
def test_count_nans_objectarray(self):
x = np.array([0, 0, 1, 2, np.nan, 2], dtype=object)
expected = 1

np.testing.assert_equal(bincount(x)[1], expected)

@dense_sparse
def test_adds_empty_bins(self, array):
x = array([0, 1, 3, 5])
Expand Down