Skip to content

Commit 94758c6

Browse files
committed
Add implementation of unique functions from Python array API
1 parent b6cc5fe commit 94758c6

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ class InsertDeleteParams(NamedTuple):
7171
usm_type: str
7272

7373

74+
# pylint:disable=missing-class-docstring
75+
class UniqueAllResult(NamedTuple):
76+
values: dpnp.ndarray
77+
indices: dpnp.ndarray
78+
inverse_indices: dpnp.ndarray
79+
counts: dpnp.ndarray
80+
81+
82+
class UniqueCountsResult(NamedTuple):
83+
values: dpnp.ndarray
84+
counts: dpnp.ndarray
85+
86+
87+
class UniqueInverseResult(NamedTuple):
88+
values: dpnp.ndarray
89+
inverse_indices: dpnp.ndarray
90+
91+
7492
__all__ = [
7593
"append",
7694
"array_split",
@@ -122,6 +140,10 @@ class InsertDeleteParams(NamedTuple):
122140
"transpose",
123141
"trim_zeros",
124142
"unique",
143+
"unique_all",
144+
"unique_counts",
145+
"unique_inverse",
146+
"unique_values",
125147
"unstack",
126148
"vsplit",
127149
"vstack",
@@ -4276,6 +4298,189 @@ def unique(
42764298
return _unpack_tuple(result)
42774299

42784300

4301+
def unique_all(x, /):
4302+
"""
4303+
Find the unique elements of an array, and counts, inverse, and indices.
4304+
4305+
For full documentation refer to :obj:`numpy.unique_all`.
4306+
4307+
Parameters
4308+
----------
4309+
x : {dpnp.ndarray, usm_ndarray}
4310+
Input array. It will be flattened if it is not already 1-D.
4311+
4312+
Returns
4313+
-------
4314+
A namedtuple with the following attributes:
4315+
4316+
values : dpnp.ndarray
4317+
The unique elements of an input array.
4318+
indices : dpnp.ndarray
4319+
The first occurring indices for each unique element.
4320+
inverse_indices : dpnp.ndarray
4321+
The indices from the set of unique elements that reconstruct `x`.
4322+
counts : dpnp.ndarray
4323+
The corresponding counts for each unique element.
4324+
4325+
See Also
4326+
--------
4327+
:obj:`dpnp.unique` : Find the unique elements of an array.
4328+
4329+
Examples
4330+
--------
4331+
>>> import dpnp as np
4332+
>>> x = np.array([1, 1, 2])
4333+
>>> uniq = np.unique_all(x)
4334+
>>> uniq.values
4335+
array([1, 2])
4336+
>>> uniq.indices
4337+
array([0, 2])
4338+
>>> uniq.inverse_indices
4339+
array([0, 0, 1])
4340+
>>> uniq.counts
4341+
array([2, 1])
4342+
4343+
"""
4344+
4345+
result = dpnp.unique(
4346+
x,
4347+
return_index=True,
4348+
return_inverse=True,
4349+
return_counts=True,
4350+
equal_nan=False,
4351+
)
4352+
return UniqueAllResult(*result)
4353+
4354+
4355+
def unique_counts(x, /):
4356+
"""
4357+
Find the unique elements and counts of an input array `x`.
4358+
4359+
For full documentation refer to :obj:`numpy.unique_counts`.
4360+
4361+
Parameters
4362+
----------
4363+
x : {dpnp.ndarray, usm_ndarray}
4364+
Input array. It will be flattened if it is not already 1-D.
4365+
4366+
Returns
4367+
-------
4368+
A namedtuple with the following attributes:
4369+
4370+
values : dpnp.ndarray
4371+
The unique elements of an input array.
4372+
counts : dpnp.ndarray
4373+
The corresponding counts for each unique element.
4374+
4375+
See Also
4376+
--------
4377+
:obj:`dpnp.unique` : Find the unique elements of an array.
4378+
4379+
Examples
4380+
--------
4381+
>>> import dpnp as np
4382+
>>> x = np.array([1, 1, 2])
4383+
>>> uniq = np.unique_counts(x)
4384+
>>> uniq.values
4385+
array([1, 2])
4386+
>>> uniq.counts
4387+
array([2, 1])
4388+
4389+
"""
4390+
4391+
result = dpnp.unique(
4392+
x,
4393+
return_index=False,
4394+
return_inverse=False,
4395+
return_counts=True,
4396+
equal_nan=False,
4397+
)
4398+
return UniqueCountsResult(*result)
4399+
4400+
4401+
def unique_inverse(x, /):
4402+
"""
4403+
Find the unique elements of `x` and indices to reconstruct `x`.
4404+
4405+
For full documentation refer to :obj:`numpy.unique_inverse`.
4406+
4407+
Parameters
4408+
----------
4409+
x : {dpnp.ndarray, usm_ndarray}
4410+
Input array. It will be flattened if it is not already 1-D.
4411+
4412+
Returns
4413+
-------
4414+
A namedtuple with the following attributes:
4415+
4416+
values : dpnp.ndarray
4417+
The unique elements of an input array.
4418+
inverse_indices : dpnp.ndarray
4419+
The indices from the set of unique elements that reconstruct `x`.
4420+
4421+
See Also
4422+
--------
4423+
:obj:`dpnp.unique` : Find the unique elements of an array.
4424+
4425+
Examples
4426+
--------
4427+
>>> import dpnp as np
4428+
>>> x = np.array([1, 1, 2])
4429+
>>> uniq = np.unique_inverse(x)
4430+
>>> uniq.values
4431+
array([1, 2])
4432+
>>> uniq.inverse_indices
4433+
array([0, 0, 1])
4434+
4435+
"""
4436+
4437+
result = dpnp.unique(
4438+
x,
4439+
return_index=False,
4440+
return_inverse=True,
4441+
return_counts=False,
4442+
equal_nan=False,
4443+
)
4444+
return UniqueInverseResult(*result)
4445+
4446+
4447+
def unique_values(x, /):
4448+
"""
4449+
Returns the unique elements of an input array `x`.
4450+
4451+
For full documentation refer to :obj:`numpy.unique_values`.
4452+
4453+
Parameters
4454+
----------
4455+
x : {dpnp.ndarray, usm_ndarray}
4456+
Input array. It will be flattened if it is not already 1-D.
4457+
4458+
Returns
4459+
-------
4460+
out : dpnp.ndarray
4461+
The unique elements of an input array.
4462+
4463+
See Also
4464+
--------
4465+
:obj:`dpnp.unique` : Find the unique elements of an array.
4466+
4467+
Examples
4468+
--------
4469+
>>> import dpnp as np
4470+
>>> np.unique_values(np.array([1, 1, 2]))
4471+
array([1, 2])
4472+
4473+
"""
4474+
4475+
return dpnp.unique(
4476+
x,
4477+
return_index=False,
4478+
return_inverse=False,
4479+
return_counts=False,
4480+
equal_nan=False,
4481+
)
4482+
4483+
42794484
def unstack(x, /, *, axis=0):
42804485
"""
42814486
Split an array into a sequence of arrays along the given axis.

0 commit comments

Comments
 (0)