Skip to content

Commit

Permalink
Merge pull request #3831 from lanzagar/bincount
Browse files Browse the repository at this point in the history
[FIX] util: Fix bincount for object arrays
  • Loading branch information
janezd authored Jun 2, 2019
2 parents bd10ccd + 1c914f6 commit 8f4aa5f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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

0 comments on commit 8f4aa5f

Please sign in to comment.