Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
17 changes: 17 additions & 0 deletions Numpy/Array Indexing and Slicing/Indexing Basics/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type: edu
files:
- name: task.py
visible: true
placeholders:
- offset: 240
length: 7
placeholder_text: '# TODO'
- offset: 252
length: 11
placeholder_text: '# TODO'
- name: tests/test_task.py
visible: false
- name: __init__.py
visible: false
- name: tests/__init__.py
visible: false
73 changes: 73 additions & 0 deletions Numpy/Array Indexing and Slicing/Indexing Basics/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## Indexing Basics

Array indexing refers to any use of the square brackets (`[]`) to index array values.

### Single element indexing

Single element indexing for a 1-D array works exactly like that
for other standard Python sequences. It is 0-based, and accepts negative indices for
indexing from the end of the array.

Unlike lists and tuples, NumPy arrays support multidimensional indexing for multidimensional
arrays. That means that **it is not necessary to separate each dimension’s index into its
own set of square brackets**.

```python
x = np.arange(10).reshape(2, 5) # 2-dimensional
print(x[1, 3])
print(x[1, -1])
print(x[0])
```
Output:
```text
8
9
[0, 1, 2, 3, 4]
```
Note that `x[0, 2]` and `x[0][2]` will produce the same result, `2`. However, the second case
is less efficient as a new temporary array is created after the first index that is subsequently
indexed by 2.

### Slicing

It is possible to slice arrays to extract arrays of the same number of dimensions,
but of different sizes than the original. The slicing works exactly the same way it
does for lists and tuples except that they can be applied to multiple dimensions as well.
Basic slicing occurs when obj is a slice object (constructed by `start:stop:step` notation inside
of brackets), an integer, or a tuple of slice objects and integers.

1-D array:
```python
x = np.arange(35)
print(x[2:15])
print(x[1:30:5]) # [start:stop:step]
```
Output:
```text
[ 2 3 4 5 6 7 8 9 10 11 12 13 14]
[ 1 6 11 16 21 26]
```
2-D array:
```python
y = x.reshape(5, 7)
print(y[1:5:2])
print(y[1:5:2, ::3]) # First slicing in the first dimension, then in the second.
```
Output:
```text
[[ 7 8 9 10 11 12 13]
[21 22 23 24 25 26 27]]
[[ 7 10 13]
[21 24 27]]
```

These examples show the use of indexing when referencing data in an array.
They work just as well when assigning to an array.
Read more about this topic [here](https://numpy.org/doc/stable/user/basics.indexing.html#basics-indexing).

### Task
1. Variable `a`: use indexing on the array `x` to extract number `19` from it. Please
**do not** just assign `a` the value `19` :)
2. Variable `b`: use slicing to extract every second element in every second row. The resulting array
should have shape `(5, 2)`.

21 changes: 21 additions & 0 deletions Numpy/Array Indexing and Slicing/Indexing Basics/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import numpy as np

x = np.arange(40).reshape(10, 4)
# Array x:
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]
# [12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]
# [24 25 26 27]
# [28 29 30 31]
# [32 33 34 35]
# [36 37 38 39]]

a = x[4, 3]
b = x[::2, ::2]

if __name__ == '__main__':
print(a)
print(b)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import unittest
import numpy as np

from task import a, b, x


class TestCase(unittest.TestCase):
def test_arrays_shape(self):
self.assertEqual(b.shape, (5, 2), msg="Wrong shape of array b")
self.assertEqual(type(a), np.int64, msg="a has to be a numpy.int64")

def test_array_content(self):
self.assertEqual(a, 19, msg="a has to be equal to 19")
np.testing.assert_array_equal(b, x[::2, ::2], err_msg='Something wrong in array b!')

2 changes: 2 additions & 0 deletions Numpy/Array Indexing and Slicing/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
content:
- Indexing Basics
1 change: 1 addition & 0 deletions Numpy/section-info.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
content:
- Introduction
- Array Basics
- Array Indexing and Slicing