Skip to content

Commit 087016d

Browse files
authored
Merge branch 'master' into sofia/transpose
2 parents 3ee650e + 4b11647 commit 087016d

File tree

20 files changed

+385
-1
lines changed

20 files changed

+385
-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+

Numpy/Array Indexing and Slicing/Indexing Basics/__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: 240
7+
length: 7
8+
placeholder_text: '# TODO'
9+
- offset: 252
10+
length: 11
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## Indexing Basics
2+
3+
Array indexing refers to any use of the square brackets (`[]`) to index array values.
4+
5+
### Single element indexing
6+
7+
Single element indexing for a 1-D array works exactly like that
8+
for other standard Python sequences. It is 0-based, and accepts negative indices for
9+
indexing from the end of the array.
10+
11+
Unlike lists and tuples, NumPy arrays support multidimensional indexing for multidimensional
12+
arrays. That means that **it is not necessary to separate each dimension’s index into its
13+
own set of square brackets**.
14+
15+
```python
16+
x = np.arange(10).reshape(2, 5) # 2-dimensional
17+
print(x[1, 3])
18+
print(x[1, -1])
19+
print(x[0])
20+
```
21+
Output:
22+
```text
23+
8
24+
9
25+
[0, 1, 2, 3, 4]
26+
```
27+
Note that `x[0, 2]` and `x[0][2]` will produce the same result, `2`. However, the second case
28+
is less efficient as a new temporary array is created after the first index that is subsequently
29+
indexed by 2.
30+
31+
### Slicing
32+
33+
It is possible to slice arrays to extract arrays of the same number of dimensions,
34+
but of different sizes than the original. The slicing works exactly the same way it
35+
does for lists and tuples except that they can be applied to multiple dimensions as well.
36+
Basic slicing occurs when obj is a slice object (constructed by `start:stop:step` notation inside
37+
of brackets), an integer, or a tuple of slice objects and integers.
38+
39+
1-D array:
40+
```python
41+
x = np.arange(35)
42+
print(x[2:15])
43+
print(x[1:30:5]) # [start:stop:step]
44+
```
45+
Output:
46+
```text
47+
[ 2 3 4 5 6 7 8 9 10 11 12 13 14]
48+
[ 1 6 11 16 21 26]
49+
```
50+
2-D array:
51+
```python
52+
y = x.reshape(5, 7)
53+
print(y[1:5:2])
54+
print(y[1:5:2, ::3]) # First slicing in the first dimension, then in the second.
55+
```
56+
Output:
57+
```text
58+
[[ 7 8 9 10 11 12 13]
59+
[21 22 23 24 25 26 27]]
60+
[[ 7 10 13]
61+
[21 24 27]]
62+
```
63+
64+
These examples show the use of indexing when referencing data in an array.
65+
They work just as well when assigning to an array.
66+
Read more about this topic [here](https://numpy.org/doc/stable/user/basics.indexing.html#basics-indexing).
67+
68+
### Task
69+
1. Variable `a`: use indexing on the array `x` to extract number `19` from it. Please
70+
**do not** just assign `a` the value `19` :)
71+
2. Variable `b`: use slicing to extract every second element in every second row. The resulting array
72+
should have shape `(5, 2)`.
73+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
3+
x = np.arange(40).reshape(10, 4)
4+
# Array x:
5+
# [[ 0 1 2 3]
6+
# [ 4 5 6 7]
7+
# [ 8 9 10 11]
8+
# [12 13 14 15]
9+
# [16 17 18 19]
10+
# [20 21 22 23]
11+
# [24 25 26 27]
12+
# [28 29 30 31]
13+
# [32 33 34 35]
14+
# [36 37 38 39]]
15+
16+
a = x[4, 3]
17+
b = x[::2, ::2]
18+
19+
if __name__ == '__main__':
20+
print(a)
21+
print(b)

0 commit comments

Comments
 (0)