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`. + +
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.compare_a_b, use the np.equal function.