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.
20 changes: 20 additions & 0 deletions Numpy/Transposing, Sorting, Concatenating/Transpose/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type: edu
files:
- name: task.py
visible: true
placeholders:
- offset: 147
length: 7
placeholder_text: '# TODO'
- offset: 204
length: 17
placeholder_text: '# TODO'
- offset: 227
length: 42
placeholder_text: '# TODO'
- name: tests/test_task.py
visible: false
- name: __init__.py
visible: false
- name: tests/__init__.py
visible: false
49 changes: 49 additions & 0 deletions Numpy/Transposing, Sorting, Concatenating/Transpose/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Transpose

When you transpose an array, the row and column indices of every
element are switched. Item `[0, 1]`, for example, becomes item `[1, 0]`.
[`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.
Alternatively you can also use the attribute [`a.T`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.T.html#numpy.ndarray.T).

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.
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.
To illustrate:

```python
a = np.arange(15).reshape(3, 5)
print(a.transpose())
print(a.T)
print(a.transpose(1, 0))
```
Output (all print the same thing):
```text
[[ 0 5 10]
[ 1 6 11]
[ 2 7 12]
[ 3 8 13]
[ 4 9 14]]
```

> <i>Method [`numpy.ndarray.reshape`](course://Numpy/Array Basics/Reshape) we encountered earlier
gives a new shape to an array without changing its data and can also be used for transposing.</i>

### Task
1. Transpose the array `a`.
2. Transform array `b` so that it looks like this:
```text
[[0 1 2]
[3 4 5]
[6 7 8]]
```
3. Use numpy methods `arange`, `reshape` and `transpose` to acquire a 3-D array `c` that looks like this:
```text
[[[ 0 4 8]
[ 2 6 10]]

[[ 1 5 9]
[ 3 7 11]]]
```

<div class="hint">You can use either <code>a.T</code> or <code>a.transpose()</code> for the first two arrays.</div>

<div class="hint">In #3 use the methods in the suggested order. You can do it in one line of code.</div>
18 changes: 18 additions & 0 deletions Numpy/Transposing, Sorting, Concatenating/Transpose/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np

a = np.array([['clear', 'usually', 'of'],
['conscience', 'the', 'bad'],
['is', 'sign', 'memory']])
a = a.T

b = np.array([[0, 3, 6], [1, 4, 7], [2, 5, 8]])
b = b.transpose()

c = np.arange(12).reshape(3, 2, 2).transpose()


if __name__ == '__main__':
print(a)
print(b)
print(c)

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
import numpy as np

from task import a, b, c


class TestCase(unittest.TestCase):
def test_arrays(self):
b_test = np.array([[0, 3, 6], [1, 4, 7], [2, 5, 8]]).transpose()
c_test = np.arange(12).reshape(3, 2, 2).transpose()
a_test = np.array([['clear', 'conscience', 'is'],
['usually', 'the', 'sign'],
['of', 'bad', 'memory']])
np.testing.assert_array_equal(a, a_test, err_msg='Array `a` is off!')
np.testing.assert_array_equal(b, b_test, err_msg='Array `b` is off!')
np.testing.assert_array_equal(c, c_test, err_msg='Array `c` is off!')

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