Skip to content

Commit c283608

Browse files
t-tasinrossbar
andauthored
DOC: Update np.unique_all example to demonstrate namedtuple output (numpy#27385)
Update docstrings for unique_* functions to emphasize nametuple outputs Co-authored-by: Ross Barnowski <[email protected]>
1 parent 88a649b commit c283608

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

numpy/lib/_arraysetops_impl.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,14 @@ def _unique_all_dispatcher(x, /):
412412
@array_function_dispatch(_unique_all_dispatcher)
413413
def unique_all(x):
414414
"""
415-
Find the unique elements of an array, and counts, inverse and indices.
415+
Find the unique elements of an array, and counts, inverse, and indices.
416416
417-
This function is an Array API compatible alternative to:
417+
This function is an Array API compatible alternative to::
418418
419-
>>> x = np.array([1, 1, 2])
420-
>>> np.unique(x, return_index=True, return_inverse=True,
421-
... return_counts=True, equal_nan=False)
422-
(array([1, 2]), array([0, 2]), array([0, 0, 1]), array([2, 1]))
419+
np.unique(x, return_index=True, return_inverse=True,
420+
return_counts=True, equal_nan=False)
421+
422+
but returns a namedtuple for easier access to each output.
423423
424424
Parameters
425425
----------
@@ -444,12 +444,16 @@ def unique_all(x):
444444
Examples
445445
--------
446446
>>> import numpy as np
447-
>>> np.unique_all([1, 1, 2])
448-
UniqueAllResult(values=array([1, 2]),
449-
indices=array([0, 2]),
450-
inverse_indices=array([0, 0, 1]),
451-
counts=array([2, 1]))
452-
447+
>>> x = [1, 1, 2]
448+
>>> uniq = np.unique_all(x)
449+
>>> uniq.values
450+
array([1, 2])
451+
>>> uniq.indices
452+
array([0, 2])
453+
>>> uniq.inverse_indices
454+
array([0, 0, 1])
455+
>>> uniq.counts
456+
array([2, 1])
453457
"""
454458
result = unique(
455459
x,
@@ -470,11 +474,11 @@ def unique_counts(x):
470474
"""
471475
Find the unique elements and counts of an input array `x`.
472476
473-
This function is an Array API compatible alternative to:
477+
This function is an Array API compatible alternative to::
474478
475-
>>> x = np.array([1, 1, 2])
476-
>>> np.unique(x, return_counts=True, equal_nan=False)
477-
(array([1, 2]), array([2, 1]))
479+
np.unique(x, return_counts=True, equal_nan=False)
480+
481+
but returns a namedtuple for easier access to each output.
478482
479483
Parameters
480484
----------
@@ -496,9 +500,12 @@ def unique_counts(x):
496500
Examples
497501
--------
498502
>>> import numpy as np
499-
>>> np.unique_counts([1, 1, 2])
500-
UniqueCountsResult(values=array([1, 2]), counts=array([2, 1]))
501-
503+
>>> x = [1, 1, 2]
504+
>>> uniq = np.unique_counts(x)
505+
>>> uniq.values
506+
array([1, 2])
507+
>>> uniq.counts
508+
array([2, 1])
502509
"""
503510
result = unique(
504511
x,
@@ -519,11 +526,11 @@ def unique_inverse(x):
519526
"""
520527
Find the unique elements of `x` and indices to reconstruct `x`.
521528
522-
This function is Array API compatible alternative to:
529+
This function is an Array API compatible alternative to::
530+
531+
np.unique(x, return_inverse=True, equal_nan=False)
523532
524-
>>> x = np.array([1, 1, 2])
525-
>>> np.unique(x, return_inverse=True, equal_nan=False)
526-
(array([1, 2]), array([0, 0, 1]))
533+
but returns a namedtuple for easier access to each output.
527534
528535
Parameters
529536
----------
@@ -546,9 +553,12 @@ def unique_inverse(x):
546553
Examples
547554
--------
548555
>>> import numpy as np
549-
>>> np.unique_inverse([1, 1, 2])
550-
UniqueInverseResult(values=array([1, 2]), inverse_indices=array([0, 0, 1]))
551-
556+
>>> x = [1, 1, 2]
557+
>>> uniq = np.unique_inverse(x)
558+
>>> uniq.values
559+
array([1, 2])
560+
>>> uniq.inverse_indices
561+
array([0, 0, 1])
552562
"""
553563
result = unique(
554564
x,
@@ -569,11 +579,9 @@ def unique_values(x):
569579
"""
570580
Returns the unique elements of an input array `x`.
571581
572-
This function is Array API compatible alternative to:
582+
This function is an Array API compatible alternative to::
573583
574-
>>> x = np.array([1, 1, 2])
575-
>>> np.unique(x, equal_nan=False)
576-
array([1, 2])
584+
np.unique(x, equal_nan=False)
577585
578586
Parameters
579587
----------

0 commit comments

Comments
 (0)