Skip to content

Commit eced588

Browse files
committed
Added Reshape task
1 parent 96ba0d5 commit eced588

File tree

7 files changed

+88
-0
lines changed

7 files changed

+88
-0
lines changed

Numpy/Array Basics/Reshape/__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: 20
8+
placeholder_text: '# TODO'
9+
- offset: 49
10+
length: 15
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

Numpy/Array Basics/Reshape/task.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Reshape
2+
3+
Reshaping means changing the `shape` of an array without changing its data.
4+
As we mentioned earlier, the `shape` of an array is the number of elements in each dimension.
5+
By reshaping we can add or remove dimensions or change the number of elements in each dimension.
6+
7+
The function [`numpy.reshape`](https://numpy.org/doc/stable/reference/generated/numpy.reshape.html#numpy.reshape)
8+
is used for reshaping, and it accepts three arguments:
9+
10+
- The array to be reshaped.
11+
- The new shape as an `int` or tuple of `int`s.
12+
- An optional argument `order`, which defines the order in which the elements are read and placed into the reshaped array.
13+
14+
### Examples
15+
1. Reshape 1D array into a 2D array:
16+
```python
17+
a = np.arange(10)
18+
print(np.reshape(a, (2, 5)))
19+
```
20+
Output:
21+
```text
22+
[[0 1 2 3 4]
23+
[5 6 7 8 9]]
24+
```
25+
2. Reshape a 2D array into a 1D array:
26+
```python
27+
a = np.array([[1, 2, 3], [4, 5, 6]])
28+
print(np.reshape(a, 6))
29+
```
30+
Output:
31+
```text
32+
[1 2 3 4 5 6]
33+
```
34+
35+
The latter can also be achieved by using `numpy.ndarray.flatten`:
36+
```python
37+
a = np.array([[1, 2, 3], [4, 5, 6]])
38+
print(a.flatten())
39+
```
40+
Output:
41+
```text
42+
[1 2 3 4 5 6]
43+
```
44+
45+
### Task
46+
1. Create an array `a` of integers in the interval from `12` to `30` with step `3`.
47+
2. Reshape it so that it has 2 rows and 3 columns.
48+
49+
<div class="hint">For (1) you can use the <code>arange</code> function.</div>

Numpy/Array Basics/Reshape/task.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
3+
a = np.arange(12, 30, 3)
4+
b = a.reshape(2, 3)
5+
6+
if __name__ == "__main__":
7+
print(a)
8+
print('shape : ', a.shape)
9+
print('\nAfter reshaping : ')
10+
print(b)
11+
print('shape : ', b.shape)

Numpy/Array Basics/Reshape/tests/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
import numpy as np
3+
4+
from task import a, b
5+
6+
class TestCase(unittest.TestCase):
7+
def test_arrays(self):
8+
np.testing.assert_array_equal(a, np.arange(12, 30, 3), err_msg='Array a is not what we expected!')
9+
np.testing.assert_array_equal(b, a.reshape(2, 3), err_msg='Array b is not what we expected!')
10+

Numpy/Array Basics/lesson-info.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ content:
33
- Create an Array from List
44
- Create an Empty Array
55
- Create an Array from Range
6+
- Reshape
67
- Random Sampling

0 commit comments

Comments
 (0)