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)
| 1265 | |
| 1266 | |
| 1267 | def unique(ar1, return_index=False, return_inverse=False): |
| 1268 | """ |
| 1269 | Finds the unique elements of an array. |
| 1270 | |
| 1271 | Masked values are considered the same element (masked). The output array |
| 1272 | is always a masked array. See `numpy.unique` for more details. |
| 1273 | |
| 1274 | See Also |
| 1275 | -------- |
| 1276 | numpy.unique : Equivalent function for ndarrays. |
| 1277 | |
| 1278 | Examples |
| 1279 | -------- |
| 1280 | >>> import numpy as np |
| 1281 | >>> a = [1, 2, 1000, 2, 3] |
| 1282 | >>> mask = [0, 0, 1, 0, 0] |
| 1283 | >>> masked_a = np.ma.masked_array(a, mask) |
| 1284 | >>> masked_a |
| 1285 | masked_array(data=[1, 2, --, 2, 3], |
| 1286 | mask=[False, False, True, False, False], |
| 1287 | fill_value=999999) |
| 1288 | >>> np.ma.unique(masked_a) |
| 1289 | masked_array(data=[1, 2, 3, --], |
| 1290 | mask=[False, False, False, True], |
| 1291 | fill_value=999999) |
| 1292 | >>> np.ma.unique(masked_a, return_index=True) |
| 1293 | (masked_array(data=[1, 2, 3, --], |
| 1294 | mask=[False, False, False, True], |
| 1295 | fill_value=999999), array([0, 1, 4, 2])) |
| 1296 | >>> np.ma.unique(masked_a, return_inverse=True) |
| 1297 | (masked_array(data=[1, 2, 3, --], |
| 1298 | mask=[False, False, False, True], |
| 1299 | fill_value=999999), array([0, 1, 3, 1, 2])) |
| 1300 | >>> np.ma.unique(masked_a, return_index=True, return_inverse=True) |
| 1301 | (masked_array(data=[1, 2, 3, --], |
| 1302 | mask=[False, False, False, True], |
| 1303 | fill_value=999999), array([0, 1, 4, 2]), array([0, 1, 3, 1, 2])) |
| 1304 | """ |
| 1305 | output = np.unique(ar1, |
| 1306 | return_index=return_index, |
| 1307 | return_inverse=return_inverse) |
| 1308 | if isinstance(output, tuple): |
| 1309 | output = list(output) |
| 1310 | output[0] = output[0].view(MaskedArray) |
| 1311 | output = tuple(output) |
| 1312 | else: |
| 1313 | output = output.view(MaskedArray) |
| 1314 | return output |
| 1315 | |
| 1316 | |
| 1317 | def intersect1d(ar1, ar2, assume_unique=False): |
searching dependent graphs…