MCPcopy Create free account
hub / github.com/numpy/numpy / unique

Function unique

numpy/lib/arraysetops.py:139–320  ·  view source on GitHub ↗

Find the unique elements of an array. Returns the sorted unique elements of an array. There are three optional outputs in addition to the unique elements: * the indices of the input array that give the unique values * the indices of the unique array that reconstruct the input

(ar, return_index=False, return_inverse=False,
           return_counts=False, axis=None, *, equal_nan=True)

Source from the content-addressed store, hash-verified

137
138@array_function_dispatch(_unique_dispatcher)
139def unique(ar, return_index=False, return_inverse=False,
140 return_counts=False, axis=None, *, equal_nan=True):
141 """
142 Find the unique elements of an array.
143
144 Returns the sorted unique elements of an array. There are three optional
145 outputs in addition to the unique elements:
146
147 * the indices of the input array that give the unique values
148 * the indices of the unique array that reconstruct the input array
149 * the number of times each unique value comes up in the input array
150
151 Parameters
152 ----------
153 ar : array_like
154 Input array. Unless `axis` is specified, this will be flattened if it
155 is not already 1-D.
156 return_index : bool, optional
157 If True, also return the indices of `ar` (along the specified axis,
158 if provided, or in the flattened array) that result in the unique array.
159 return_inverse : bool, optional
160 If True, also return the indices of the unique array (for the specified
161 axis, if provided) that can be used to reconstruct `ar`.
162 return_counts : bool, optional
163 If True, also return the number of times each unique item appears
164 in `ar`.
165 axis : int or None, optional
166 The axis to operate on. If None, `ar` will be flattened. If an integer,
167 the subarrays indexed by the given axis will be flattened and treated
168 as the elements of a 1-D array with the dimension of the given axis,
169 see the notes for more details. Object arrays or structured arrays
170 that contain objects are not supported if the `axis` kwarg is used. The
171 default is None.
172
173 .. versionadded:: 1.13.0
174
175 equal_nan : bool, optional
176 If True, collapses multiple NaN values in the return array into one.
177
178 .. versionadded:: 1.24
179
180 Returns
181 -------
182 unique : ndarray
183 The sorted unique values.
184 unique_indices : ndarray, optional
185 The indices of the first occurrences of the unique values in the
186 original array. Only provided if `return_index` is True.
187 unique_inverse : ndarray, optional
188 The indices to reconstruct the original array from the
189 unique array. Only provided if `return_inverse` is True.
190 unique_counts : ndarray, optional
191 The number of times each of the unique values comes up in the
192 original array. Only provided if `return_counts` is True.
193
194 .. versionadded:: 1.9.0
195
196 See Also

Callers 12

test_simpleMethod · 0.90
check_allMethod · 0.90
test_unique_1dMethod · 0.90
test_unique_axis_listMethod · 0.90
test_unique_axisMethod · 0.90
_run_axis_testsMethod · 0.90
intersect1dFunction · 0.70
setxor1dFunction · 0.70
union1dFunction · 0.70
setdiff1dFunction · 0.70

Calls 6

_unique1dFunction · 0.85
reshape_uniqFunction · 0.85
reshapeMethod · 0.80
_unpack_tupleFunction · 0.70
prodMethod · 0.45
viewMethod · 0.45

Tested by 8

test_simpleMethod · 0.72
check_allMethod · 0.72
test_unique_1dMethod · 0.72
test_unique_axis_listMethod · 0.72
test_unique_axisMethod · 0.72
_run_axis_testsMethod · 0.72