Use an index array to construct a new array from a list of choices. Given an array of integers and a list of n choice arrays, this method will create a new array that merges each of the choice arrays. Where a value in `index` is i, the new array will have the value that choices[i]
(indices, choices, out=None, mode='raise')
| 7692 | |
| 7693 | |
| 7694 | def choose(indices, choices, out=None, mode='raise'): |
| 7695 | """ |
| 7696 | Use an index array to construct a new array from a list of choices. |
| 7697 | |
| 7698 | Given an array of integers and a list of n choice arrays, this method |
| 7699 | will create a new array that merges each of the choice arrays. Where a |
| 7700 | value in `index` is i, the new array will have the value that choices[i] |
| 7701 | contains in the same place. |
| 7702 | |
| 7703 | Parameters |
| 7704 | ---------- |
| 7705 | indices : ndarray of ints |
| 7706 | This array must contain integers in ``[0, n-1]``, where n is the |
| 7707 | number of choices. |
| 7708 | choices : sequence of arrays |
| 7709 | Choice arrays. The index array and all of the choices should be |
| 7710 | broadcastable to the same shape. |
| 7711 | out : array, optional |
| 7712 | If provided, the result will be inserted into this array. It should |
| 7713 | be of the appropriate shape and `dtype`. |
| 7714 | mode : {'raise', 'wrap', 'clip'}, optional |
| 7715 | Specifies how out-of-bounds indices will behave. |
| 7716 | |
| 7717 | * 'raise' : raise an error |
| 7718 | * 'wrap' : wrap around |
| 7719 | * 'clip' : clip to the range |
| 7720 | |
| 7721 | Returns |
| 7722 | ------- |
| 7723 | merged_array : array |
| 7724 | |
| 7725 | See Also |
| 7726 | -------- |
| 7727 | choose : equivalent function |
| 7728 | |
| 7729 | Examples |
| 7730 | -------- |
| 7731 | >>> choice = np.array([[1,1,1], [2,2,2], [3,3,3]]) |
| 7732 | >>> a = np.array([2, 1, 0]) |
| 7733 | >>> np.ma.choose(a, choice) |
| 7734 | masked_array(data=[3, 2, 1], |
| 7735 | mask=False, |
| 7736 | fill_value=999999) |
| 7737 | |
| 7738 | """ |
| 7739 | def fmask(x): |
| 7740 | "Returns the filled array, or True if masked." |
| 7741 | if x is masked: |
| 7742 | return True |
| 7743 | return filled(x) |
| 7744 | |
| 7745 | def nmask(x): |
| 7746 | "Returns the mask, True if ``masked``, False if ``nomask``." |
| 7747 | if x is masked: |
| 7748 | return True |
| 7749 | return getmask(x) |
| 7750 | # Get the indices. |
| 7751 | c = filled(indices, 0) |