Create a boolean mask from an array. Return `m` as a boolean mask, creating a copy if necessary or requested. The function can accept any sequence that is convertible to integers, or ``nomask``. Does not require that contents must be 0s and 1s, values of 0 are interpreted as F
(m, copy=False, shrink=True, dtype=MaskType)
| 1553 | |
| 1554 | |
| 1555 | def make_mask(m, copy=False, shrink=True, dtype=MaskType): |
| 1556 | """ |
| 1557 | Create a boolean mask from an array. |
| 1558 | |
| 1559 | Return `m` as a boolean mask, creating a copy if necessary or requested. |
| 1560 | The function can accept any sequence that is convertible to integers, |
| 1561 | or ``nomask``. Does not require that contents must be 0s and 1s, values |
| 1562 | of 0 are interpreted as False, everything else as True. |
| 1563 | |
| 1564 | Parameters |
| 1565 | ---------- |
| 1566 | m : array_like |
| 1567 | Potential mask. |
| 1568 | copy : bool, optional |
| 1569 | Whether to return a copy of `m` (True) or `m` itself (False). |
| 1570 | shrink : bool, optional |
| 1571 | Whether to shrink `m` to ``nomask`` if all its values are False. |
| 1572 | dtype : dtype, optional |
| 1573 | Data-type of the output mask. By default, the output mask has a |
| 1574 | dtype of MaskType (bool). If the dtype is flexible, each field has |
| 1575 | a boolean dtype. This is ignored when `m` is ``nomask``, in which |
| 1576 | case ``nomask`` is always returned. |
| 1577 | |
| 1578 | Returns |
| 1579 | ------- |
| 1580 | result : ndarray |
| 1581 | A boolean mask derived from `m`. |
| 1582 | |
| 1583 | Examples |
| 1584 | -------- |
| 1585 | >>> import numpy.ma as ma |
| 1586 | >>> m = [True, False, True, True] |
| 1587 | >>> ma.make_mask(m) |
| 1588 | array([ True, False, True, True]) |
| 1589 | >>> m = [1, 0, 1, 1] |
| 1590 | >>> ma.make_mask(m) |
| 1591 | array([ True, False, True, True]) |
| 1592 | >>> m = [1, 0, 2, -3] |
| 1593 | >>> ma.make_mask(m) |
| 1594 | array([ True, False, True, True]) |
| 1595 | |
| 1596 | Effect of the `shrink` parameter. |
| 1597 | |
| 1598 | >>> m = np.zeros(4) |
| 1599 | >>> m |
| 1600 | array([0., 0., 0., 0.]) |
| 1601 | >>> ma.make_mask(m) |
| 1602 | False |
| 1603 | >>> ma.make_mask(m, shrink=False) |
| 1604 | array([False, False, False, False]) |
| 1605 | |
| 1606 | Using a flexible `dtype`. |
| 1607 | |
| 1608 | >>> m = [1, 0, 1, 1] |
| 1609 | >>> n = [0, 1, 0, 0] |
| 1610 | >>> arr = [] |
| 1611 | >>> for man, mouse in zip(m, n): |
| 1612 | ... arr.append((man, mouse)) |