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

Function _unique1d

numpy/lib/arraysetops.py:323–365  ·  view source on GitHub ↗

Find the unique elements of an array, ignoring shape.

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

Source from the content-addressed store, hash-verified

321
322
323def _unique1d(ar, return_index=False, return_inverse=False,
324 return_counts=False, *, equal_nan=True):
325 """
326 Find the unique elements of an array, ignoring shape.
327 """
328 ar = np.asanyarray(ar).flatten()
329
330 optional_indices = return_index or return_inverse
331
332 if optional_indices:
333 perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
334 aux = ar[perm]
335 else:
336 ar.sort()
337 aux = ar
338 mask = np.empty(aux.shape, dtype=np.bool_)
339 mask[:1] = True
340 if (equal_nan and aux.shape[0] > 0 and aux.dtype.kind in "cfmM" and
341 np.isnan(aux[-1])):
342 if aux.dtype.kind == "c": # for complex all NaNs are considered equivalent
343 aux_firstnan = np.searchsorted(np.isnan(aux), True, side='left')
344 else:
345 aux_firstnan = np.searchsorted(aux, aux[-1], side='left')
346 if aux_firstnan > 0:
347 mask[1:aux_firstnan] = (
348 aux[1:aux_firstnan] != aux[:aux_firstnan - 1])
349 mask[aux_firstnan] = True
350 mask[aux_firstnan + 1:] = False
351 else:
352 mask[1:] = aux[1:] != aux[:-1]
353
354 ret = (aux[mask],)
355 if return_index:
356 ret += (perm[mask],)
357 if return_inverse:
358 imask = np.cumsum(mask) - 1
359 inv_idx = np.empty(mask.shape, dtype=np.intp)
360 inv_idx[perm] = imask
361 ret += (inv_idx,)
362 if return_counts:
363 idx = np.concatenate(np.nonzero(mask) + ([mask.size],))
364 ret += (np.diff(idx),)
365 return ret
366
367
368def _intersect1d_dispatcher(

Callers 1

uniqueFunction · 0.85

Calls 5

flattenMethod · 0.80
sortMethod · 0.80
cumsumMethod · 0.80
nonzeroMethod · 0.80
argsortMethod · 0.45

Tested by

no test coverage detected