From 487ef7d7194c6851aafa2cb371fe73ee0938a354 Mon Sep 17 00:00:00 2001 From: Carlos Perez Date: Sun, 12 Apr 2020 18:45:45 +0200 Subject: [PATCH 1/3] Adds alternative solutions for an exercise This adds alternatives to the reading comprehension exercise _Assignment via advanced indexing_ of the chapter _Advanced indexing_ from Module 3 --- Python/Module3_IntroducingNumpy/AdvancedIndexing.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md index 101ed3d2..98bc82b5 100644 --- a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md +++ b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md @@ -695,6 +695,15 @@ This will return a tuple of two integer-valued index-arrays. These contain the i ... [ 0.84, 0.76, 0.25, 0.07]]) >>> x[np.arange(4), np.arange(4)] = range(4) +# equivalent (works for the general case of a square matrix of N-dims) +# x[tuple(map(np.arange, x.shape))] = range(x.shape[0]) + +# same as above, using a tuple comprehension +# x[tuple(np.arange(x) for x in x.shape)] = range(x.shape[0]) + +# equivalent (using numpy built-in functions): +# x[np.diag_indices_from(x)] = np.arange(4) + >>> x[0.8 < x] += 1 >>> x array([[ 0. , 0.05, 1.84, 0.21], From 69033a86d3197b81bf363bd924f3e57e9a48e87d Mon Sep 17 00:00:00 2001 From: Carlos Perez Date: Sun, 12 Apr 2020 22:42:11 +0200 Subject: [PATCH 2/3] Adds solution with ~np.diagonal~ --- Python/Module3_IntroducingNumpy/AdvancedIndexing.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md index 98bc82b5..b97d0b16 100644 --- a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md +++ b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md @@ -703,6 +703,7 @@ This will return a tuple of two integer-valued index-arrays. These contain the i # equivalent (using numpy built-in functions): # x[np.diag_indices_from(x)] = np.arange(4) +# np.fill_diagonal(x, np.arange(4)) >>> x[0.8 < x] += 1 >>> x From 58f475f2642914c2066dd154d62363e9461439a8 Mon Sep 17 00:00:00 2001 From: Carlos Perez Date: Wed, 15 Apr 2020 10:36:32 +0200 Subject: [PATCH 3/3] Remove example with map Address comments. Remove example with `map` since it is not discussed elsewhere in PLYMI. --- Python/Module3_IntroducingNumpy/AdvancedIndexing.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md index b97d0b16..9a42f3a7 100644 --- a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md +++ b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md @@ -696,9 +696,6 @@ This will return a tuple of two integer-valued index-arrays. These contain the i >>> x[np.arange(4), np.arange(4)] = range(4) # equivalent (works for the general case of a square matrix of N-dims) -# x[tuple(map(np.arange, x.shape))] = range(x.shape[0]) - -# same as above, using a tuple comprehension # x[tuple(np.arange(x) for x in x.shape)] = range(x.shape[0]) # equivalent (using numpy built-in functions):