Skip to content

Commit 8ebc467

Browse files
committed
Added task Search to Compare and Search
1 parent 7e1e29f commit 8ebc467

File tree

7 files changed

+137
-0
lines changed

7 files changed

+137
-0
lines changed

NumPy/Compare Search/Search/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 194
7+
length: 8
8+
placeholder_text: '# TODO'
9+
- offset: 226
10+
length: 38
11+
placeholder_text: '# TODO'
12+
- offset: 277
13+
length: 23
14+
placeholder_text: '# TODO'
15+
- offset: 209
16+
length: 7
17+
placeholder_text: '# TODO'
18+
- name: tests/test_task.py
19+
visible: false
20+
- name: __init__.py
21+
visible: false
22+
- name: tests/__init__.py
23+
visible: false
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
rng = np.random.default_rng()
3+
4+
temperatures = rng.integers(25, size=7)
5+
days = np.array(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])
6+
7+
high = ['High']
8+
low = ['Low']
9+
result = np.where(temperatures > 15, high, low)
10+
warm_days = days[temperatures > 15]
11+
12+
if __name__ == '__main__':
13+
print(temperatures)
14+
print(result)
15+
print(warm_days)
16+
17+

NumPy/Compare Search/Search/tests/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
import numpy as np
3+
4+
from task import temperatures, days, high, low, result, warm_days
5+
6+
7+
class TestCase(unittest.TestCase):
8+
def test_week(self):
9+
high_test = ['High']
10+
low_test = ['Low']
11+
np.testing.assert_array_equal(result, np.where(temperatures > 15, high_test, low_test),
12+
err_msg='Your `result` array '
13+
'does not contain the values we expected.')
14+
self.assertEqual(result.shape, days.shape, msg='Shape of the array `result` should match the shape of `days`.')
15+
16+
def test_names(self):
17+
np.testing.assert_array_equal(warm_days, days[temperatures > 15],
18+
err_msg='`warm_days` array is off. It should contain '
19+
'the names of days when temperature was higher than 15')

NumPy/Compare Search/lesson-info.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ content:
33
- Compare with a scalar
44
- Element-wise Comparison
55
- Find maximum
6+
- Search

0 commit comments

Comments
 (0)