From 3ee650e95f1f32e5f06db566126ae5600724e524 Mon Sep 17 00:00:00 2001 From: sofiiako <19rubisco93@gmail.com> Date: Tue, 24 Aug 2021 20:55:45 +0300 Subject: [PATCH] Added Transpose task to section Transposing, Sorting and Concatenating (should be #1) --- .../Transpose/__init__.py | 0 .../Transpose/task-info.yaml | 20 ++++++++ .../Transpose/task.md | 49 +++++++++++++++++++ .../Transpose/task.py | 18 +++++++ .../Transpose/tests/__init__.py | 0 .../Transpose/tests/test_task.py | 17 +++++++ .../lesson-info.yaml | 2 + Numpy/section-info.yaml | 1 + 8 files changed, 107 insertions(+) create mode 100644 Numpy/Transposing, Sorting, Concatenating/Transpose/__init__.py create mode 100644 Numpy/Transposing, Sorting, Concatenating/Transpose/task-info.yaml create mode 100644 Numpy/Transposing, Sorting, Concatenating/Transpose/task.md create mode 100644 Numpy/Transposing, Sorting, Concatenating/Transpose/task.py create mode 100644 Numpy/Transposing, Sorting, Concatenating/Transpose/tests/__init__.py create mode 100644 Numpy/Transposing, Sorting, Concatenating/Transpose/tests/test_task.py create mode 100644 Numpy/Transposing, Sorting, Concatenating/lesson-info.yaml diff --git a/Numpy/Transposing, Sorting, Concatenating/Transpose/__init__.py b/Numpy/Transposing, Sorting, Concatenating/Transpose/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Numpy/Transposing, Sorting, Concatenating/Transpose/task-info.yaml b/Numpy/Transposing, Sorting, Concatenating/Transpose/task-info.yaml new file mode 100644 index 0000000..2b772ca --- /dev/null +++ b/Numpy/Transposing, Sorting, Concatenating/Transpose/task-info.yaml @@ -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 diff --git a/Numpy/Transposing, Sorting, Concatenating/Transpose/task.md b/Numpy/Transposing, Sorting, Concatenating/Transpose/task.md new file mode 100644 index 0000000..7e3de5a --- /dev/null +++ b/Numpy/Transposing, Sorting, Concatenating/Transpose/task.md @@ -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]] +``` + +> 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. + +### 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]]] +``` + +
a.T or a.transpose() for the first two arrays.