Skip to content

Commit 80d497d

Browse files
authored
add bincount function (#796)
* add bincount function
1 parent 4756f55 commit 80d497d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

dpnp/dpnp_iface_statistics.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
'amax',
5252
'amin',
5353
'average',
54+
'bincount',
5455
'correlate',
5556
'cov',
5657
'max',
@@ -169,6 +170,28 @@ def average(x1, axis=None, weights=None, returned=False):
169170
return call_origin(numpy.average, x1, axis, weights, returned)
170171

171172

173+
def bincount(x1, weights=None, minlength=0):
174+
"""
175+
Count number of occurrences of each value in array of non-negative ints.
176+
177+
For full documentation refer to :obj:`numpy.bincount`.
178+
179+
See Also
180+
--------
181+
:obj:`dpnp.unique` : Find the unique elements of an array.
182+
183+
Examples
184+
--------
185+
>>> import dpnp as np
186+
>>> res = np.bincount(np.arange(5))
187+
>>> print(res)
188+
[1, 1, 1, 1, 1]
189+
190+
"""
191+
192+
return call_origin(numpy.bincount, x1, weights=weights, minlength=minlength)
193+
194+
172195
def correlate(x1, x2, mode='valid'):
173196
"""
174197
Cross-correlation of two 1-dimensional sequences.

tests/test_statistics.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,42 @@ def test_nanvar(array):
7272
expected = numpy.nanvar(a, axis=None, ddof=0)
7373
result = dpnp.nanvar(ia, axis=None, ddof=0)
7474
numpy.testing.assert_array_equal(expected, result)
75+
76+
77+
class TestBincount:
78+
79+
@pytest.mark.parametrize("array",
80+
[[1, 2, 3],
81+
[1, 2, 2, 1, 2, 4],
82+
[2, 2, 2, 2]],
83+
ids=['[1, 2, 3]',
84+
'[1, 2, 2, 1, 2, 4]',
85+
'[2, 2, 2, 2]'])
86+
@pytest.mark.parametrize("minlength",
87+
[0, 1, 3, 5],
88+
ids=['0', '1', '3', '5'])
89+
def test_bincount_minlength(self, array, minlength):
90+
np_a = numpy.array(array)
91+
dpnp_a = dpnp.array(array)
92+
93+
expected = numpy.bincount(np_a, minlength=minlength)
94+
result = dpnp.bincount(dpnp_a, minlength=minlength)
95+
numpy.testing.assert_array_equal(expected, result)
96+
97+
@pytest.mark.parametrize("array",
98+
[[1, 2, 2, 1, 2, 4]],
99+
ids=['[1, 2, 2, 1, 2, 4]'])
100+
@pytest.mark.parametrize("weights",
101+
[None,
102+
[0.3, 0.5, 0.2, 0.7, 1., -0.6],
103+
[2, 2, 2, 2, 2, 2]],
104+
ids=['None',
105+
'[0.3, 0.5, 0.2, 0.7, 1., -0.6]',
106+
'[2, 2, 2, 2, 2, 2]'])
107+
def test_bincount_weights(self, array, weights):
108+
np_a = numpy.array(array)
109+
dpnp_a = dpnp.array(array)
110+
111+
expected = numpy.bincount(np_a, weights=weights)
112+
result = dpnp.bincount(dpnp_a, weights=weights)
113+
numpy.testing.assert_array_equal(expected, result)

0 commit comments

Comments
 (0)