Skip to content

Commit 4eb21b7

Browse files
authored
Merge pull request #5 from jetbrains-academy/sofia/integer_array_indexing
Added Integer Array Indexing task to section Array Indexing ans Slici…
2 parents 9c95d9b + 57ed12f commit 4eb21b7

File tree

7 files changed

+157
-0
lines changed

7 files changed

+157
-0
lines changed

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

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 63
7+
length: 28
8+
placeholder_text: '# TODO: use a 1-D numpy array for indexing'
9+
- offset: 96
10+
length: 52
11+
placeholder_text: '# TODO: use a 2-D numpy array for indexing'
12+
- offset: 155
13+
length: 22
14+
placeholder_text: '# TODO: use a 1-D numpy array for indexing'
15+
- offset: 182
16+
length: 43
17+
placeholder_text: '# TODO: use two 1-D arrays for indexing'
18+
- offset: 230
19+
length: 25
20+
placeholder_text: '# TODO: use a 1-D numpy array and a scalar for indexing'
21+
- name: tests/test_task.py
22+
visible: false
23+
- name: __init__.py
24+
visible: false
25+
- name: tests/__init__.py
26+
visible: false
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
## Integer array indexing
2+
3+
NumPy arrays may be indexed with other arrays.
4+
For all cases of index arrays, what is returned is a copy of the original data, not a view as one gets for slices.
5+
6+
Index arrays must be of integer type. Each value in the array indicates which value in the array
7+
to use in place of the index. Negative values are permitted and work as they do with single indices or slices.
8+
9+
```python
10+
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
11+
a = np.array([0, 3, 5, 2])
12+
print(x[a])
13+
```
14+
Output:
15+
```text
16+
[1 4 6 3]
17+
```
18+
19+
When index arrays are used, what is returned is an array with the same shape as the index
20+
array, but with the type and values of the array being indexed:
21+
```python
22+
b = np.array([[9, 9], [0, 1]])
23+
c = x[b]
24+
print(c)
25+
```
26+
Output:
27+
```text
28+
[[10 10]
29+
[ 1 2]]
30+
```
31+
32+
### Indexing multidimensional arrays
33+
Things become more complex when multidimensional arrays are indexed, particularly with multidimensional index arrays.
34+
Let's consider this:
35+
36+
```python
37+
x = np.arange(15).reshape(5, 3)
38+
print(x)
39+
print(x[np.array([0, 1, 4])]) # 1
40+
print(x[np.array([0, 1, 4]), np.array([0, 0, 1])]) # 2
41+
```
42+
Output:
43+
```text
44+
[[ 0 1 2]
45+
[ 3 4 5]
46+
[ 6 7 8]
47+
[ 9 10 11]
48+
[12 13 14]]
49+
50+
[[ 0 1 2]
51+
[ 3 4 5]
52+
[12 13 14]]
53+
54+
[ 0 3 13]
55+
```
56+
You can see that in the first case, a new array is constructed where
57+
each value of the index array selects one row from the indexed array and the resulting
58+
array has the shape `(number_of_index_elements, size_of_row)`.
59+
60+
In the second case, the resulting array has the same shape as the index arrays, and the values
61+
correspond to the index set for each position in the index arrays. In this example, the first
62+
index value is `0` for both index arrays, and thus the first value of the resulting array is `x[0, 0]`.
63+
The next value is `y[1, 0]`, and the last is `y[4, 1]`.
64+
65+
If the index arrays do not have the same shape, there is an attempt to broadcast them to the same shape.
66+
If they cannot be broadcast to the same shape, an exception is raised.
67+
68+
The broadcasting mechanism permits index arrays to be combined with scalars for other indices.
69+
The scalar value is used for all the corresponding values of the index arrays:
70+
71+
```python
72+
print(x[np.array([0, 2, 3, 4]), 1])
73+
```
74+
Output:
75+
```text
76+
[ 1 7 10 13]
77+
```
78+
You can read more about integer indexing [here](https://numpy.org/doc/stable/reference/arrays.indexing.html#integer-array-indexing).
79+
80+
### Task
81+
1. Using integer array indexing create an array `a` that contains elements with
82+
indices `[7, 13, 28, 33]` from the array `x`.
83+
Then create a 2-D array `b` with shape `(3, 3)` that contains elements with indices
84+
`[0, 1, 2], [10, 11, 12], [28, 29, 30]` from the array `x`.
85+
86+
2. Based on the 2-D array `y`:
87+
- Create an array `c` containing rows number `0`, `2` and `4`.
88+
- Create a 1-D array `d`, containing elements `0`, `1` and `2` from the rows `0`, `2` and `4`, respectively.
89+
- Create a 1-D array `e`, containing elements with index `6` from rows `1`, `2` and `4`.
90+
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(35)
4+
y = x.reshape(5, 7)
5+
6+
a = x[np.array([7, 13, 28, 33])]
7+
b = x[np.array([[0, 1, 2], [10, 11, 12], [28, 29, 30]])]
8+
9+
10+
c = y[np.array([0, 2, 4])]
11+
d = y[np.array([0, 2, 4]), np.array([0, 1, 2])]
12+
e = y[np.array([1, 2, 4]), 6]
13+
14+
15+
if __name__ == '__main__':
16+
print(y)
17+
print('\n', a.shape)
18+
print('\n', b.shape)
19+
print('\n', c.shape)
20+
print('\n', d.shape)
21+
print('\n', e.shape)

Numpy/Array Indexing and Slicing/Integer Array Indexing/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 x, y, a, b, c, d, e
5+
6+
class TestCase(unittest.TestCase):
7+
def test_shape(self):
8+
self.assertEqual(a.shape, (4,), msg='Wrong shape of array a!')
9+
self.assertEqual(b.shape, (3, 3), msg='Wrong shape of array b!')
10+
self.assertEqual(c.shape, (3, 7), msg='Wrong shape of array c!')
11+
self.assertEqual(d.shape, (3,), msg='Wrong shape of array d!')
12+
self.assertEqual(e.shape, (3,), msg='Wrong shape of array e!')
13+
14+
def test_arrays(self):
15+
np.testing.assert_array_equal(a, x[np.array([7, 13, 28, 33])], err_msg='Array a is not what we expected!')
16+
np.testing.assert_array_equal(b, x[np.array([[0, 1, 2], [10, 11, 12], [28, 29, 30]])], err_msg='Array b is not what we expected!')
17+
np.testing.assert_array_equal(c, y[np.array([0, 2, 4])], err_msg='Array c is not what we expected!')
18+
np.testing.assert_array_equal(d, y[np.array([0, 2, 4]), np.array([0, 1, 2])], err_msg='Array d is not what we expected!')
19+
np.testing.assert_array_equal(e, y[np.array([1, 2, 4]), 6], err_msg='Array e is not what we expected!')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
content:
22
- Indexing Basics
3+
- Integer Array Indexing

0 commit comments

Comments
 (0)