From 4e3bb6275162bb63bc8a009621398316692a89eb Mon Sep 17 00:00:00 2001 From: sofiiako <19rubisco93@gmail.com> Date: Wed, 22 Sep 2021 16:53:54 +0300 Subject: [PATCH] Added task element-wise comparison --- .../Element-wise Comparison/__init__.py | 0 .../Element-wise Comparison/task-info.yaml | 17 ++++++ .../Element-wise Comparison/task.md | 57 +++++++++++++++++++ .../Element-wise Comparison/task.py | 15 +++++ .../Element-wise Comparison/tests/__init__.py | 0 .../tests/test_task.py | 15 +++++ Numpy/Compare, Search/lesson-info.yaml | 1 + 7 files changed, 105 insertions(+) create mode 100644 Numpy/Compare, Search/Element-wise Comparison/__init__.py create mode 100644 Numpy/Compare, Search/Element-wise Comparison/task-info.yaml create mode 100644 Numpy/Compare, Search/Element-wise Comparison/task.md create mode 100644 Numpy/Compare, Search/Element-wise Comparison/task.py create mode 100644 Numpy/Compare, Search/Element-wise Comparison/tests/__init__.py create mode 100644 Numpy/Compare, Search/Element-wise Comparison/tests/test_task.py diff --git a/Numpy/Compare, Search/Element-wise Comparison/__init__.py b/Numpy/Compare, Search/Element-wise Comparison/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Numpy/Compare, Search/Element-wise Comparison/task-info.yaml b/Numpy/Compare, Search/Element-wise Comparison/task-info.yaml new file mode 100644 index 0000000..73e0181 --- /dev/null +++ b/Numpy/Compare, Search/Element-wise Comparison/task-info.yaml @@ -0,0 +1,17 @@ +type: edu +files: +- name: task.py + visible: true + placeholders: + - offset: 56 + length: 19 + placeholder_text: '# TODO' + - offset: 90 + length: 14 + 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/Compare, Search/Element-wise Comparison/task.md b/Numpy/Compare, Search/Element-wise Comparison/task.md new file mode 100644 index 0000000..b65fcf1 --- /dev/null +++ b/Numpy/Compare, Search/Element-wise Comparison/task.md @@ -0,0 +1,57 @@ +## Element-wise comparison + +Element-wise comparison is when you compare each element of one array to an element of another array. +[`numpy.equal`](https://numpy.org/doc/stable/reference/generated/numpy.equal.html) is the function that returns `(x1 == x2)` element-wise. +It accepts two input arrays, which must either have equal `shape` or be broadcastable to a common shape +(which becomes the shape of the output). Elements of the output array are typically boolean values. + +Example: +```python +print(np.equal([0, 1, 3], np.arange(3))) +``` +Output: +```text +[ True, True, False] +``` +What is compared are values, not types. So an int (1) and an array of length one can evaluate as `True`: +```python +print(np.equal(1, np.ones(1))) +``` +Output: +```text +[ True] +``` +The `==` operator can be used as a shorthand for `np.equal` on ndarrays. + +### `numpy.array_equal` + +[`numpy.array_equal`](https://numpy.org/doc/stable/reference/generated/numpy.array_equal.html) is the +function that returns `True` if two input arrays have **the same shape and elements**, and `False` otherwise. + +Examples: +```python +print(np.array_equal([1, 2], [1, 2])) +print(np.array_equal(np.array([1, 2]), np.array([1, 2]))) +print(np.array_equal([1, 2], [1, 2, 3])) +print(np.array_equal([1, 2], [1, 4])) +``` +Output: +```text +True +True +False +False +``` + +### Task +Define a **1-D** array `b`, such that by comparing it element-wise with the array `a` you can +get the array `compare_a_b`, which is `array_equal` (has the same elements and shape) to the array in the last +`print` statement. When you run the script, the last `print` statement has to print `True`. + +
Array b should have be of shape (5,). Use +np.arange with start, stop and step arguments. +By looking an the resulting boolean array you can figure out which integer has to be in +what position in b and then come up with suitable arguments for the np.arange +function.
+ +
To get the array compare_a_b, use the np.equal function.
diff --git a/Numpy/Compare, Search/Element-wise Comparison/task.py b/Numpy/Compare, Search/Element-wise Comparison/task.py new file mode 100644 index 0000000..3880370 --- /dev/null +++ b/Numpy/Compare, Search/Element-wise Comparison/task.py @@ -0,0 +1,15 @@ +import numpy as np + +a = np.arange(20).reshape(4, 5) +b = np.arange(0, 25, 6) +compare_a_b = np.equal(a, b) + + +if __name__ == '__main__': + print(a) + print(b) + print(np.array_equal(compare_a_b, np.array([[True, False, False, False, False], + [False, True, False, False, False], + [False, False, True, False, False], + [False, False, False, True, False]]))) + diff --git a/Numpy/Compare, Search/Element-wise Comparison/tests/__init__.py b/Numpy/Compare, Search/Element-wise Comparison/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/Numpy/Compare, Search/Element-wise Comparison/tests/test_task.py b/Numpy/Compare, Search/Element-wise Comparison/tests/test_task.py new file mode 100644 index 0000000..7f219c5 --- /dev/null +++ b/Numpy/Compare, Search/Element-wise Comparison/tests/test_task.py @@ -0,0 +1,15 @@ +import unittest +import numpy as np + +from task import * + + +class TestCase(unittest.TestCase): + def test_array_b(self): + test_b = np.arange(0, 25, 6) + self.assertEqual(b.shape, test_b.shape, msg='Shape of array b has to be (5,).') + + def test_array_c(self): + test_b = np.arange(0, 25, 6) + test_c = np.equal(a, test_b) + np.testing.assert_array_equal(compare_a_b, test_c, err_msg='Boolean arrays do not match.') diff --git a/Numpy/Compare, Search/lesson-info.yaml b/Numpy/Compare, Search/lesson-info.yaml index 2a9f3d5..8ca6307 100644 --- a/Numpy/Compare, Search/lesson-info.yaml +++ b/Numpy/Compare, Search/lesson-info.yaml @@ -1,2 +1,3 @@ content: - Compare with a scalar +- Element-wise Comparison