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.
17 changes: 17 additions & 0 deletions Numpy/Compare, Search/Element-wise Comparison/task-info.yaml
Original file line number Diff line number Diff line change
@@ -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
57 changes: 57 additions & 0 deletions Numpy/Compare, Search/Element-wise Comparison/task.md
Original file line number Diff line number Diff line change
@@ -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`.

<div class="hint">Array <code>b</code> should have be of shape <code>(5,)</code>. Use
<code>np.arange</code> with <code>start</code>, <code>stop</code> and <code>step</code> arguments.
By looking an the resulting boolean array you can figure out which integer has to be in
what position in <code>b</code> and then come up with suitable arguments for the <code>np.arange</code>
function.</div>

<div class="hint">To get the array <code>compare_a_b</code>, use the <code>np.equal</code> function.</div>
15 changes: 15 additions & 0 deletions Numpy/Compare, Search/Element-wise Comparison/task.py
Original file line number Diff line number Diff line change
@@ -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]])))

Empty file.
15 changes: 15 additions & 0 deletions Numpy/Compare, Search/Element-wise Comparison/tests/test_task.py
Original file line number Diff line number Diff line change
@@ -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.')
1 change: 1 addition & 0 deletions Numpy/Compare, Search/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
content:
- Compare with a scalar
- Element-wise Comparison