Browse Source

feat(numpy): update exercise 6

- revert question 2 to its original version
- add question 3 to practice broadcasting
pull/2304/head
nprimo 6 months ago committed by Niccolò Primo
parent
commit
95bf292c56
  1. 16
      subjects/ai/numpy/README.md
  2. 30
      subjects/ai/numpy/audit/README.md

16
subjects/ai/numpy/README.md

@ -156,7 +156,7 @@ The goal of this exercise is to learn to concatenate and reshape arrays.
The goal of this exercise is to learn to access values of n-dimensional arrays efficiently.
1. Create an 2-dimensional array size 9,9 of 1s. Each value has to be an `int8`.
2. Using a combination of **slicing** and **broadcasting**, output this array:
2. Using **slicing**, output this array:
```python
array([[1, 1, 1, 1, 1, 1, 1, 1, 1],
@ -170,6 +170,20 @@ The goal of this exercise is to learn to access values of n-dimensional arrays e
[1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
```
3. Using **broadcasting** create the ouptu matrix starting from these two arrays:
```python
array_1 = np.array([1,2,3,4,5], dtype=int8)
array_2 = np.array([1,2,3], dtype=int8)
...
# output matrix
array([[ 1, 2, 3],
[ 2, 4, 6],
[ 3, 6, 9],
[ 4, 8, 12],
[ 5, 10, 15]], dtype=int8)
```
https://jakevdp.github.io/PythonDataScienceHandbook/ (section: Computation on Arrays: Broadcasting)
---

30
subjects/ai/numpy/audit/README.md

@ -186,17 +186,33 @@ https://jakevdp.github.io/PythonDataScienceHandbook/ (section: The Basics of Num
[1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
```
##### The solution of question 2 is not accepted if the values of the array have been changed one by one manually.
##### The solution of question 2 is not accepted if the values of the array have been changed one by one manually. The usage of the for loop is not allowed neither.
Here is an example of a possible solution:
```python
for step in range(1, 5):
array_len = 9 - step * 2
if step % 2 == 1:
x[step:-step, step:-step] -= np.ones(array_len, dtype='int8')
else:
x[step:-step, step:-step] += np.ones(array_len, dtype='int8')
x[1:8,1:8] = 0
x[2:7,2:7] = 1
x[3:6,3:6] = 0
x[4,4] = 1
```
###### For question 3, is the output the following?
```console
array([[ 1, 2, 3],
[ 2, 4, 6],
[ 3, 6, 9],
[ 4, 8, 12],
[ 5, 10, 15]], dtype=int8)
```
##### The solution of question 3 is not accepted if the values of the array have been changed one by one manually. The usage of the for loop is not allowed neither.
Here is an example of a possible solution:
```python
np.reshape(arr_1, (5, 1)) * arr_2
```
---

Loading…
Cancel
Save