|
| 1 | +## Search |
| 2 | + |
| 3 | +You can search an array for a certain value, or for values that satisfy a condition, |
| 4 | +and return the **indices** that get a match. |
| 5 | + |
| 6 | +You can use the [`numpy.nonzero`](https://numpy.org/doc/stable/reference/generated/numpy.nonzero.html) method: |
| 7 | + |
| 8 | +```python |
| 9 | +a = np.array([0, 10, 4, 11, 7, 3]) |
| 10 | +print(np.nonzero(a < 5)) |
| 11 | +print(np.nonzero(a == 7)) |
| 12 | +print(a < 5) |
| 13 | +``` |
| 14 | +Output: |
| 15 | +```text |
| 16 | +(array([0, 2, 5]),) |
| 17 | +(array([4]),) |
| 18 | +[ True False True False False True] |
| 19 | +``` |
| 20 | +`numpy.nonzero` returns the indices of the elements that are non-zero. |
| 21 | +A common use for it is to find the indices of an array, where a condition is |
| 22 | +`True`. For the array `a`, the condition `a < 5` is a boolean array (see example above). |
| 23 | +`False` is interpreted as `0`, therefore `np.nonzero(a < 5)` yields the indices of |
| 24 | +the `a` where the condition is true. Using the result to index `a` (`a[np.nonzero(a < 5)]`) is equivalent to |
| 25 | +using the mask directly (`a[a < 5]`), and the latter is the preferred spelling. |
| 26 | + |
| 27 | +`np.nonzero(a < 5)` is equivalent to `np.where(a < 5)`: |
| 28 | + |
| 29 | +```python |
| 30 | +print(np.where(a < 5)) |
| 31 | +``` |
| 32 | +Output: |
| 33 | +```text |
| 34 | +(array([0, 2, 5]),) |
| 35 | +``` |
| 36 | +However, [`numpy.where`](https://numpy.org/doc/stable/reference/generated/numpy.where.html) can also be used in a different way: if provided with a condition |
| 37 | +and two input arrays, `numpy.where(condition[, x, y])` returns **elements** (not indices) chosen from |
| 38 | +`x` or `y` depending on the condition: |
| 39 | + |
| 40 | +```python |
| 41 | +a = np.array([0, 0, 0, 0, 0, 5, 6, 7, 8, 9]) |
| 42 | +b = a * 10 |
| 43 | +print(np.where(a < 5, a, b)) |
| 44 | +``` |
| 45 | +Output: |
| 46 | +```text |
| 47 | +[ 0 0 0 0 0 50 60 70 80 90] |
| 48 | +``` |
| 49 | + |
| 50 | +In fact, the first argument accepted by this function is also an array - a boolean array, `a < 5` in this case. |
| 51 | +Therefore, it can also be used like this: |
| 52 | + |
| 53 | +```python |
| 54 | +result = np.where([False, True, False], [1, 1, 1], [7, 8, 9]) |
| 55 | +print(result) |
| 56 | +``` |
| 57 | +Output: |
| 58 | +```text |
| 59 | +[7 1 9] |
| 60 | +``` |
| 61 | +`x`, `y` and `condition` arrays need to be broadcastable to some shape. |
| 62 | + |
| 63 | +### Task |
| 64 | + |
| 65 | +You are given an array of random integers between 0 and 25 representing temperatures registered in |
| 66 | +certain days of the week. Using the functionality you just learned about, generate an array `result`, |
| 67 | +which contains strings, either `Low` or `High`, in the order depending on whether the corresponding day of the |
| 68 | +week was warmer than 15 degrees (`High`) or not (`Low`). You will probably need to define two additional arrays, `high` and `low`, |
| 69 | +containing strings that will be used for the `result`. |
| 70 | + |
| 71 | +In addition, `warm_days` should contain the names of the warm days (with temperatures above 15). |
| 72 | + |
| 73 | +<div class="hint"><code>result</code> should look something like <code>['High' 'Low' 'Low' 'Low' 'High' 'High' 'High']</code>.</div> |
| 74 | + |
| 75 | +<div class="hint">Use <code>numpy.where</code> with a condition about temperatures and two input arrays <code>high</code> and <code>low</code> to get the <code>result</code>.</div> |
| 76 | + |
| 77 | +<div class="hint">For the last part, use <code>numpy.nonzero</code> or a direct mask.</div> |
0 commit comments