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

Function unique

numpy/ma/extras.py:1180–1227  ·  view source on GitHub ↗

Finds the unique elements of an array. Masked values are considered the same element (masked). The output array is always a masked array. See `numpy.unique` for more details. See Also -------- numpy.unique : Equivalent function for ndarrays. Examples --------

(ar1, return_index=False, return_inverse=False)

Source from the content-addressed store, hash-verified

1178
1179
1180def unique(ar1, return_index=False, return_inverse=False):
1181 """
1182 Finds the unique elements of an array.
1183
1184 Masked values are considered the same element (masked). The output array
1185 is always a masked array. See `numpy.unique` for more details.
1186
1187 See Also
1188 --------
1189 numpy.unique : Equivalent function for ndarrays.
1190
1191 Examples
1192 --------
1193 >>> import numpy.ma as ma
1194 >>> a = [1, 2, 1000, 2, 3]
1195 >>> mask = [0, 0, 1, 0, 0]
1196 >>> masked_a = ma.masked_array(a, mask)
1197 >>> masked_a
1198 masked_array(data=[1, 2, --, 2, 3],
1199 mask=[False, False, True, False, False],
1200 fill_value=999999)
1201 >>> ma.unique(masked_a)
1202 masked_array(data=[1, 2, 3, --],
1203 mask=[False, False, False, True],
1204 fill_value=999999)
1205 >>> ma.unique(masked_a, return_index=True)
1206 (masked_array(data=[1, 2, 3, --],
1207 mask=[False, False, False, True],
1208 fill_value=999999), array([0, 1, 4, 2]))
1209 >>> ma.unique(masked_a, return_inverse=True)
1210 (masked_array(data=[1, 2, 3, --],
1211 mask=[False, False, False, True],
1212 fill_value=999999), array([0, 1, 3, 1, 2]))
1213 >>> ma.unique(masked_a, return_index=True, return_inverse=True)
1214 (masked_array(data=[1, 2, 3, --],
1215 mask=[False, False, False, True],
1216 fill_value=999999), array([0, 1, 4, 2]), array([0, 1, 3, 1, 2]))
1217 """
1218 output = np.unique(ar1,
1219 return_index=return_index,
1220 return_inverse=return_inverse)
1221 if isinstance(output, tuple):
1222 output = list(output)
1223 output[0] = output[0].view(MaskedArray)
1224 output = tuple(output)
1225 else:
1226 output = output.view(MaskedArray)
1227 return output
1228
1229
1230def intersect1d(ar1, ar2, assume_unique=False):

Callers 8

test_unique_onlistMethod · 0.90
test_unique_allmaskedMethod · 0.90
intersect1dFunction · 0.70
setxor1dFunction · 0.70
in1dFunction · 0.70
union1dFunction · 0.70
setdiff1dFunction · 0.70

Calls 1

viewMethod · 0.45

Tested by 3

test_unique_onlistMethod · 0.72
test_unique_allmaskedMethod · 0.72