Skip to content

Commit 46b66f8

Browse files
authored
Merge pull request #7 from jetbrains-academy/sofia/transpose
Added Transpose task to section Transposing, Sorting and Concatenatin…
2 parents 4b11647 + 087016d commit 46b66f8

File tree

8 files changed

+107
-0
lines changed

8 files changed

+107
-0
lines changed

Numpy/Transposing, Sorting, Concatenating/Transpose/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 147
7+
length: 7
8+
placeholder_text: '# TODO'
9+
- offset: 204
10+
length: 17
11+
placeholder_text: '# TODO'
12+
- offset: 227
13+
length: 42
14+
placeholder_text: '# TODO'
15+
- name: tests/test_task.py
16+
visible: false
17+
- name: __init__.py
18+
visible: false
19+
- name: tests/__init__.py
20+
visible: false
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Transpose
2+
3+
When you transpose an array, the row and column indices of every
4+
element are switched. Item `[0, 1]`, for example, becomes item `[1, 0]`.
5+
[`numpy.ndarray.transpose`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.transpose.html#numpy.ndarray.transpose) returns a view of the array with axes transposed.
6+
Alternatively you can also use the attribute [`a.T`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.T.html#numpy.ndarray.T).
7+
8+
This method accepts `None` or a tuple of `int`s. In the case of `None` or no argument it reverses the order of the axes.
9+
In the case of a tuple of `int`s: `i` in the `j`-th place in the tuple means that `a`’s `i`-th axis becomes `a.transpose()`’s `j`-th axis.
10+
To illustrate:
11+
12+
```python
13+
a = np.arange(15).reshape(3, 5)
14+
print(a.transpose())
15+
print(a.T)
16+
print(a.transpose(1, 0))
17+
```
18+
Output (all print the same thing):
19+
```text
20+
[[ 0 5 10]
21+
[ 1 6 11]
22+
[ 2 7 12]
23+
[ 3 8 13]
24+
[ 4 9 14]]
25+
```
26+
27+
> <i>Method [`numpy.ndarray.reshape`](course://Numpy/Array Basics/Reshape) we encountered earlier
28+
gives a new shape to an array without changing its data and can also be used for transposing.</i>
29+
30+
### Task
31+
1. Transpose the array `a`.
32+
2. Transform array `b` so that it looks like this:
33+
```text
34+
[[0 1 2]
35+
[3 4 5]
36+
[6 7 8]]
37+
```
38+
3. Use numpy methods `arange`, `reshape` and `transpose` to acquire a 3-D array `c` that looks like this:
39+
```text
40+
[[[ 0 4 8]
41+
[ 2 6 10]]
42+
43+
[[ 1 5 9]
44+
[ 3 7 11]]]
45+
```
46+
47+
<div class="hint">You can use either <code>a.T</code> or <code>a.transpose()</code> for the first two arrays.</div>
48+
49+
<div class="hint">In #3 use the methods in the suggested order. You can do it in one line of code.</div>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
3+
a = np.array([['clear', 'usually', 'of'],
4+
['conscience', 'the', 'bad'],
5+
['is', 'sign', 'memory']])
6+
a = a.T
7+
8+
b = np.array([[0, 3, 6], [1, 4, 7], [2, 5, 8]])
9+
b = b.transpose()
10+
11+
c = np.arange(12).reshape(3, 2, 2).transpose()
12+
13+
14+
if __name__ == '__main__':
15+
print(a)
16+
print(b)
17+
print(c)
18+

Numpy/Transposing, Sorting, Concatenating/Transpose/tests/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
import numpy as np
3+
4+
from task import a, b, c
5+
6+
7+
class TestCase(unittest.TestCase):
8+
def test_arrays(self):
9+
b_test = np.array([[0, 3, 6], [1, 4, 7], [2, 5, 8]]).transpose()
10+
c_test = np.arange(12).reshape(3, 2, 2).transpose()
11+
a_test = np.array([['clear', 'conscience', 'is'],
12+
['usually', 'the', 'sign'],
13+
['of', 'bad', 'memory']])
14+
np.testing.assert_array_equal(a, a_test, err_msg='Array `a` is off!')
15+
np.testing.assert_array_equal(b, b_test, err_msg='Array `b` is off!')
16+
np.testing.assert_array_equal(c, c_test, err_msg='Array `c` is off!')
17+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
content:
2+
- Transpose

Numpy/section-info.yaml

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

0 commit comments

Comments
 (0)