Skip to content

Commit 4b11647

Browse files
authored
Merge pull request #6 from jetbrains-academy/sofia/boolean_indexing
Added Boolean Array Indexing task to section Array Indexing and Slici…
2 parents 4eb21b7 + fe1949a commit 4b11647

File tree

7 files changed

+99
-1
lines changed

7 files changed

+99
-1
lines changed

Numpy/Array Indexing and Slicing/Boolean Indexing/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 24
7+
length: 30
8+
placeholder_text: '# TODO'
9+
- offset: 62
10+
length: 40
11+
placeholder_text: '# TODO'
12+
- name: tests/test_task.py
13+
visible: false
14+
- name: __init__.py
15+
visible: false
16+
- name: tests/__init__.py
17+
visible: false
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Boolean Indexing
2+
3+
Boolean arrays used as indices are treated differently from index
4+
arrays. Boolean arrays must be of the same shape as the initial dimensions of the array being indexed.
5+
Such boolean arrays are often referred to as **masks** and the process of indexing other arrays using them
6+
is called masking, or masked filtering.
7+
8+
```python
9+
y = np.arange(20).reshape(5, 4)
10+
b = y % 2 != 0
11+
print(b)
12+
print(y[b])
13+
```
14+
Output:
15+
```text
16+
[[False True False True]
17+
[False True False True]
18+
[False True False True]
19+
[False True False True]
20+
[False True False True]]
21+
[ 1 3 5 7 9 11 13 15 17 19]
22+
```
23+
Unlike in the case of integer index arrays, in the boolean case, the result is a 1-D array
24+
containing all the elements in the indexed array corresponding to all the true elements in the boolean array.
25+
As with index arrays, what is returned is a copy of the data, not a view as one gets with slices.
26+
27+
28+
The result will be multidimensional if `y` has more dimensions than `b`. For example, if we use a
29+
1-D boolean array where the second dimension agrees with that of `b` (just selecting one row from `b`),
30+
then the columns from `y` corresponding to the `True` values in the 1-D array will be selected:
31+
```python
32+
print(b[1, :])
33+
print(y[:, b[1, :]])
34+
```
35+
Output:
36+
```text
37+
[False True False True]
38+
[[ 1 3]
39+
[ 5 7]
40+
[ 9 11]
41+
[13 15]
42+
[17 19]]
43+
```
44+
The 2nd and 4th rows are selected from the indexed array and combined to make a 2-D array.
45+
46+
In general, when the boolean array has fewer dimensions than the indexed array,
47+
the shape of the result is one dimension containing the number of `True` elements
48+
of the boolean array, followed by the remaining dimensions of the indexed array.
49+
50+
For further details, consult the NumPy reference documentation on [array indexing](https://numpy.org/doc/stable/reference/arrays.indexing.html#indexing).
51+
52+
### Task
53+
1. Create a 3-D array `a` with integers from 0 to 19 and shape `(2, 2, 5)`.
54+
2. Filter it using a 2-D boolean array `b` so that the resulting 2-D array `c` has the
55+
shape `(2, 5)` and contains elements `[ 0 1 2 3 4]` and `[15 16 17 18 19]`.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
3+
a = np.arange(20).reshape(2, 2, 5)
4+
mask = np.array([[True, False], [False, True]])
5+
c = a[mask]
6+
7+
if __name__ == '__main__':
8+
print('Array a:\n', a, '\n')
9+
print('Indexed array c:\n', c)
10+
print(c.shape)

Numpy/Array Indexing and Slicing/Boolean Indexing/tests/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
import numpy as np
3+
4+
from task import a, mask, c
5+
6+
7+
class TestCase(unittest.TestCase):
8+
def test_add(self):
9+
test_a = np.arange(20).reshape(2, 2, 5)
10+
test_b = np.array([[True, False], [False, True]])
11+
np.testing.assert_array_equal(a, test_a, err_msg='Array `a` seems off!')
12+
np.testing.assert_array_equal(c, test_a[test_b], err_msg='Array `c` seems off!')
13+
np.testing.assert_array_equal(mask, test_b, err_msg='Something wrong with the mask array!')
14+
15+
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
content:
22
- Indexing Basics
3-
- Integer Array Indexing
3+
- Integer Array Indexing
4+
- Boolean Indexing

0 commit comments

Comments
 (0)