Skip to content

Commit 509ea3e

Browse files
authored
Merge pull request #15 from jetbrains-academy/sofia/element-wise_comparison
Added task element-wise comparison
2 parents 57c5eeb + 4e3bb62 commit 509ea3e

File tree

7 files changed

+105
-0
lines changed

7 files changed

+105
-0
lines changed

Numpy/Compare, Search/Element-wise Comparison/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 56
7+
length: 19
8+
placeholder_text: '# TODO'
9+
- offset: 90
10+
length: 14
11+
placeholder_text: '# TODO'
12+
- name: tests/test_task.py
13+
visible: false
14+
- name: __init__.py
15+
visible: false
16+
- name: tests/__init__.py
17+
visible: false
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## Element-wise comparison
2+
3+
Element-wise comparison is when you compare each element of one array to an element of another array.
4+
[`numpy.equal`](https://numpy.org/doc/stable/reference/generated/numpy.equal.html) is the function that returns `(x1 == x2)` element-wise.
5+
It accepts two input arrays, which must either have equal `shape` or be broadcastable to a common shape
6+
(which becomes the shape of the output). Elements of the output array are typically boolean values.
7+
8+
Example:
9+
```python
10+
print(np.equal([0, 1, 3], np.arange(3)))
11+
```
12+
Output:
13+
```text
14+
[ True, True, False]
15+
```
16+
What is compared are values, not types. So an int (1) and an array of length one can evaluate as `True`:
17+
```python
18+
print(np.equal(1, np.ones(1)))
19+
```
20+
Output:
21+
```text
22+
[ True]
23+
```
24+
The `==` operator can be used as a shorthand for `np.equal` on ndarrays.
25+
26+
### `numpy.array_equal`
27+
28+
[`numpy.array_equal`](https://numpy.org/doc/stable/reference/generated/numpy.array_equal.html) is the
29+
function that returns `True` if two input arrays have **the same shape and elements**, and `False` otherwise.
30+
31+
Examples:
32+
```python
33+
print(np.array_equal([1, 2], [1, 2]))
34+
print(np.array_equal(np.array([1, 2]), np.array([1, 2])))
35+
print(np.array_equal([1, 2], [1, 2, 3]))
36+
print(np.array_equal([1, 2], [1, 4]))
37+
```
38+
Output:
39+
```text
40+
True
41+
True
42+
False
43+
False
44+
```
45+
46+
### Task
47+
Define a **1-D** array `b`, such that by comparing it element-wise with the array `a` you can
48+
get the array `compare_a_b`, which is `array_equal` (has the same elements and shape) to the array in the last
49+
`print` statement. When you run the script, the last `print` statement has to print `True`.
50+
51+
<div class="hint">Array <code>b</code> should have be of shape <code>(5,)</code>. Use
52+
<code>np.arange</code> with <code>start</code>, <code>stop</code> and <code>step</code> arguments.
53+
By looking an the resulting boolean array you can figure out which integer has to be in
54+
what position in <code>b</code> and then come up with suitable arguments for the <code>np.arange</code>
55+
function.</div>
56+
57+
<div class="hint">To get the array <code>compare_a_b</code>, use the <code>np.equal</code> function.</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
3+
a = np.arange(20).reshape(4, 5)
4+
b = np.arange(0, 25, 6)
5+
compare_a_b = np.equal(a, b)
6+
7+
8+
if __name__ == '__main__':
9+
print(a)
10+
print(b)
11+
print(np.array_equal(compare_a_b, np.array([[True, False, False, False, False],
12+
[False, True, False, False, False],
13+
[False, False, True, False, False],
14+
[False, False, False, True, False]])))
15+

Numpy/Compare, Search/Element-wise Comparison/tests/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
import numpy as np
3+
4+
from task import *
5+
6+
7+
class TestCase(unittest.TestCase):
8+
def test_array_b(self):
9+
test_b = np.arange(0, 25, 6)
10+
self.assertEqual(b.shape, test_b.shape, msg='Shape of array b has to be (5,).')
11+
12+
def test_array_c(self):
13+
test_b = np.arange(0, 25, 6)
14+
test_c = np.equal(a, test_b)
15+
np.testing.assert_array_equal(compare_a_b, test_c, err_msg='Boolean arrays do not match.')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
content:
22
- Compare with a scalar
3+
- Element-wise Comparison

0 commit comments

Comments
 (0)