From f0fe3f794a3e4bad5e2627516b6663a2c263604d Mon Sep 17 00:00:00 2001 From: nprimo Date: Tue, 21 Nov 2023 11:27:30 +0000 Subject: [PATCH 1/2] feat(numpy): update exercise 6 subject and audit - Make exercise relevant to the topic of broadcasting --- subjects/ai/numpy/README.md | 2 +- subjects/ai/numpy/audit/README.md | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/subjects/ai/numpy/README.md b/subjects/ai/numpy/README.md index de8bb6c046..074d88a3f5 100644 --- a/subjects/ai/numpy/README.md +++ b/subjects/ai/numpy/README.md @@ -157,7 +157,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 **slicing**, output this array: +2. Using a combination of **slicing** and **broadcasting**, output this array: ```python array([[1, 1, 1, 1, 1, 1, 1, 1, 1], diff --git a/subjects/ai/numpy/audit/README.md b/subjects/ai/numpy/audit/README.md index b4bb1782b1..86c56a55ec 100644 --- a/subjects/ai/numpy/audit/README.md +++ b/subjects/ai/numpy/audit/README.md @@ -186,15 +186,17 @@ 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 usage of the for loop is not allowed neither. +##### The solution of question 2 is not accepted if the values of the array have been changed one by one manually. Here is an example of a possible solution: -```console - x[1:8,1:8] = 0 - x[2:7,2:7] = 1 - x[3:6,3:6] = 0 - x[4,4] = 1 +```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') ``` --- From 2108c98c39a3655c6d457105cc91f71cc8ecbe70 Mon Sep 17 00:00:00 2001 From: nprimo Date: Tue, 21 Nov 2023 17:56:30 +0000 Subject: [PATCH 2/2] feat(numpy): update exercise 6 - revert question 2 to its original version - add question 3 to practice broadcasting --- subjects/ai/numpy/README.md | 16 +++++++++++++++- subjects/ai/numpy/audit/README.md | 30 +++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/subjects/ai/numpy/README.md b/subjects/ai/numpy/README.md index 074d88a3f5..81e2ed3c1d 100644 --- a/subjects/ai/numpy/README.md +++ b/subjects/ai/numpy/README.md @@ -157,7 +157,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], @@ -171,6 +171,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) --- diff --git a/subjects/ai/numpy/audit/README.md b/subjects/ai/numpy/audit/README.md index 86c56a55ec..2a314fe716 100644 --- a/subjects/ai/numpy/audit/README.md +++ b/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 ``` ---