Skip to content

Commit 9c95d9b

Browse files
authored
Merge pull request #4 from jetbrains-academy/sofia/array_indexing
Created Array indexing and slicing section;
2 parents 70b2175 + cb5cdb3 commit 9c95d9b

File tree

8 files changed

+129
-0
lines changed

8 files changed

+129
-0
lines changed

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)

Numpy/Array Indexing and Slicing/Indexing Basics/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, b, x
5+
6+
7+
class TestCase(unittest.TestCase):
8+
def test_arrays_shape(self):
9+
self.assertEqual(b.shape, (5, 2), msg="Wrong shape of array b")
10+
self.assertEqual(type(a), np.int64, msg="a has to be a numpy.int64")
11+
12+
def test_array_content(self):
13+
self.assertEqual(a, 19, msg="a has to be equal to 19")
14+
np.testing.assert_array_equal(b, x[::2, ::2], err_msg='Something wrong in array b!')
15+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
content:
2+
- Indexing Basics

Numpy/section-info.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
content:
22
- Introduction
33
- Array Basics
4+
- Array Indexing and Slicing

0 commit comments

Comments
 (0)