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]]] +``` + +
You can use either a.T or a.transpose() for the first two arrays.
+ +
In #3 use the methods in the suggested order. You can do it in one line of code.
diff --git a/Numpy/Transposing, Sorting, Concatenating/Transpose/task.py b/Numpy/Transposing, Sorting, Concatenating/Transpose/task.py new file mode 100644 index 0000000..322b723 --- /dev/null +++ b/Numpy/Transposing, Sorting, Concatenating/Transpose/task.py @@ -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) + diff --git a/Numpy/Transposing, Sorting, Concatenating/Transpose/tests/__init__.py b/Numpy/Transposing, Sorting, Concatenating/Transpose/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Numpy/Transposing, Sorting, Concatenating/Transpose/tests/test_task.py b/Numpy/Transposing, Sorting, Concatenating/Transpose/tests/test_task.py new file mode 100644 index 0000000..a8a4ba7 --- /dev/null +++ b/Numpy/Transposing, Sorting, Concatenating/Transpose/tests/test_task.py @@ -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!') + diff --git a/Numpy/Transposing, Sorting, Concatenating/lesson-info.yaml b/Numpy/Transposing, Sorting, Concatenating/lesson-info.yaml new file mode 100644 index 0000000..2837ace --- /dev/null +++ b/Numpy/Transposing, Sorting, Concatenating/lesson-info.yaml @@ -0,0 +1,2 @@ +content: +- Transpose diff --git a/Numpy/section-info.yaml b/Numpy/section-info.yaml index 918ecb8..3912573 100644 --- a/Numpy/section-info.yaml +++ b/Numpy/section-info.yaml @@ -1,3 +1,4 @@ content: - Introduction - Array Basics +- Transposing, Sorting, Concatenating