From bffecc9926f180f16e3300d27da6c2fb52b10955 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 00:23:17 +0100 Subject: [PATCH 01/11] Fix hint 2 --- source/exercises100.ktx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 1840c420..6eefc34c 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -11,7 +11,7 @@ import numpy as np Print the numpy version and the configuration (★☆☆) < h2 -hint: np.__version__, np.show_config) +hint: np.__version__, np.show_config < a2 print(np.__version__) From 4dd62650f53234f81bc8733dd56cbaad7f3c7f66 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 00:25:24 +0100 Subject: [PATCH 02/11] Fix answer 9 --- source/exercises100.ktx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 6eefc34c..8cbc42bd 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -85,8 +85,8 @@ Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆) hint: reshape < a9 -nz = np.nonzero([1,2,0,0,4,0]) -print(nz) +Z = np.arange(9).reshape((3, 3)) +print(Z) < q10 Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆) From ebd2b739454a1ecf4b6d21a54d105807788759e9 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 00:28:47 +0100 Subject: [PATCH 03/11] Optimize answer 18 --- source/exercises100.ktx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 8cbc42bd..9bbde08e 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -191,7 +191,7 @@ Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆) hint: np.diag < a18 -Z = np.diag(1+np.arange(4),k=-1) +Z = np.diag(np.arange(1,5),k=-1) print(Z) < q19 From d3a511703bf7c1e03b329679215153e5ec084f3b Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 00:44:36 +0100 Subject: [PATCH 04/11] Fix alternative answer 31 --- source/exercises100.ktx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 9bbde08e..e5967a84 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -371,8 +371,9 @@ Z = np.ones(1) / 0 _ = np.seterr(**defaults) # Equivalently with a context manager -nz = np.nonzero([1,2,0,0,4,0]) -print(nz) +with np.errstate(all='ignore'): + Z = np.arange(3) / 0 +print(Z) < q32 Is the following expressions true? (★☆☆) From 861e5d67b9168087d08d255b1da8496d1d69f464 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 00:46:50 +0100 Subject: [PATCH 05/11] Simplify setup of answer 35 --- source/exercises100.ktx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index e5967a84..ebb9e452 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -415,9 +415,9 @@ How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆) hint: np.add(out=), np.negative(out=), np.multiply(out=), np.divide(out=) < a35 -A = np.ones(3)*1 -B = np.ones(3)*2 -C = np.ones(3)*3 +A = np.ones(3) +B = np.full(3, 2) + np.add(A,B,out=B) np.divide(A,2,out=A) np.negative(A,out=A) From e5a13fdb4d10bbd95c3ce06ab13f3f6369a8b41e Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 00:55:10 +0100 Subject: [PATCH 06/11] Simplify answer 44 using np.hypot --- source/exercises100.ktx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index ebb9e452..f57c279c 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -527,12 +527,12 @@ Z[0] = 1 Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆) < h44 -hint: np.sqrt, np.arctan2 +hint: np.hypot, np.arctan2 < a44 Z = np.random.random((10,2)) X,Y = Z[:,0], Z[:,1] -R = np.sqrt(X**2+Y**2) +R = np.hypot(Y,X) T = np.arctan2(Y,X) print(R) print(T) From c36f064ab37fcb47b31bd5619564b226e5d1b0d1 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 01:10:46 +0100 Subject: [PATCH 07/11] Simplify answer 52 using np.hypot --- source/exercises100.ktx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index f57c279c..affd45ee 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -635,7 +635,7 @@ hint: np.atleast_2d, T, np.sqrt < a52 Z = np.random.random((10,2)) X,Y = np.atleast_2d(Z[:,0], Z[:,1]) -D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2) +D = np.hypot(X-X.T, Y-Y.T) print(D) # Much faster with scipy From 7d40613daaa8c2b27bbbc53a701ea2dd6ee39fa4 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Thu, 12 Mar 2020 01:18:34 +0100 Subject: [PATCH 08/11] Simplify answer 56 using np.hypot --- source/exercises100.ktx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index affd45ee..ab1855a0 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -706,7 +706,7 @@ hint: np.meshgrid, np.exp < a56 X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) -D = np.sqrt(X*X+Y*Y) +D = np.hypot(X, Y) sigma, mu = 1.0, 0.0 G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) print(G) From d43e2086339d233c79a715da001102896e8e7332 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Fri, 13 Mar 2020 15:10:43 +0100 Subject: [PATCH 09/11] Fix answer 66 * Treating `I` as `np.ubyte` causes multiplication with 256 to overflow. * There is no need to apply `np.unique` to `I. --- source/exercises100.ktx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index ab1855a0..7f51da0b 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -857,10 +857,10 @@ hint: np.unique # Author: Nadav Horesh w,h = 16,16 -I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) +I = np.random.randint(0,2,(h,w,3)) F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] n = len(np.unique(F)) -print(np.unique(I)) +print(n) < q67 Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) From 02b7c17c42d017b9f598d9f42743c6300899b532 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Sat, 14 Mar 2020 02:06:12 +0100 Subject: [PATCH 10/11] Simplify answer 78 --- source/exercises100.ktx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/exercises100.ktx b/source/exercises100.ktx index 7f51da0b..4b40217f 100644 --- a/source/exercises100.ktx +++ b/source/exercises100.ktx @@ -1046,9 +1046,9 @@ No hints provided... def distance(P0, P1, p): T = P1 - P0 L = (T**2).sum(axis=1) - U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L + U = ((P0 - p)*T).sum(axis=1) / L U = U.reshape(len(U),1) - D = P0 + U*T - p + D = P0 - U*T - p return np.sqrt((D**2).sum(axis=1)) P0 = np.random.uniform(-10,10,(10,2)) From 52fa50ed82ba00175dee8ae378955bb2780598b3 Mon Sep 17 00:00:00 2001 From: Nikolaos Korasidis Date: Sat, 14 Mar 2020 02:09:01 +0100 Subject: [PATCH 11/11] Run generators --- 100_Numpy_exercises.ipynb | 2 +- 100_Numpy_exercises.md | 2 +- 100_Numpy_exercises_with_hints.md | 8 ++-- ...mpy_exercises_with_hints_with_solutions.md | 40 +++++++++---------- 100_Numpy_exercises_with_solutions.md | 36 ++++++++--------- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 2e4a30ee..7ecf4200 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -564,7 +564,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### 36. Extract the integer part of a random array using 5 different methods (★★☆)" + "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" ] }, { diff --git a/100_Numpy_exercises.md b/100_Numpy_exercises.md index 1e7ce709..85c1eac4 100644 --- a/100_Numpy_exercises.md +++ b/100_Numpy_exercises.md @@ -113,7 +113,7 @@ np.sqrt(-1) == np.emath.sqrt(-1) #### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆) -#### 36. Extract the integer part of a random array using 5 different methods (★★☆) +#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆) #### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) diff --git a/100_Numpy_exercises_with_hints.md b/100_Numpy_exercises_with_hints.md index 96fa44af..58f97702 100644 --- a/100_Numpy_exercises_with_hints.md +++ b/100_Numpy_exercises_with_hints.md @@ -15,7 +15,7 @@ File automatically generated. See the documentation to update questions/answers/ #### 1. Import the numpy package under the name `np` (★☆☆) `hint: import … as` #### 2. Print the numpy version and the configuration (★☆☆) -`hint: np.__version__, np.show_config)` +`hint: np.__version__, np.show_config` #### 3. Create a null vector of size 10 (★☆☆) `hint: np.zeros` #### 4. How to find the memory size of any array (★☆☆) @@ -113,8 +113,8 @@ np.sqrt(-1) == np.emath.sqrt(-1) `hint: np.arange(dtype=datetime64['D'])` #### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆) `hint: np.add(out=), np.negative(out=), np.multiply(out=), np.divide(out=)` -#### 36. Extract the integer part of a random array using 5 different methods (★★☆) -`hint: %, np.floor, np.ceil, astype, np.trunc` +#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆) +`hint: %, np.floor, astype, np.trunc` #### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) `hint: np.arange` #### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆) @@ -130,7 +130,7 @@ np.sqrt(-1) == np.emath.sqrt(-1) #### 43. Make an array immutable (read-only) (★★☆) `hint: flags.writeable` #### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆) -`hint: np.sqrt, np.arctan2` +`hint: np.hypot, np.arctan2` #### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆) `hint: argmax` #### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆) diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 8319fdbf..f9fb8f6d 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -19,7 +19,7 @@ File automatically generated. See the documentation to update questions/answers/ import numpy as np ``` #### 2. Print the numpy version and the configuration (★☆☆) -`hint: np.__version__, np.show_config)` +`hint: np.__version__, np.show_config` ```python print(np.__version__) @@ -72,8 +72,8 @@ print(Z) `hint: reshape` ```python -nz = np.nonzero([1,2,0,0,4,0]) -print(nz) +Z = np.arange(9).reshape((3, 3)) +print(Z) ``` #### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆) `hint: np.nonzero` @@ -151,7 +151,7 @@ print(0.3 == 3 * 0.1) `hint: np.diag` ```python -Z = np.diag(1+np.arange(4),k=-1) +Z = np.diag(np.arange(1,5),k=-1) print(Z) ``` #### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆) @@ -292,8 +292,9 @@ Z = np.ones(1) / 0 _ = np.seterr(**defaults) # Equivalently with a context manager -nz = np.nonzero([1,2,0,0,4,0]) -print(nz) +with np.errstate(all='ignore'): + Z = np.arange(3) / 0 +print(Z) ``` #### 32. Is the following expressions true? (★☆☆) ```python @@ -323,23 +324,22 @@ print(Z) `hint: np.add(out=), np.negative(out=), np.multiply(out=), np.divide(out=)` ```python -A = np.ones(3)*1 -B = np.ones(3)*2 -C = np.ones(3)*3 +A = np.ones(3) +B = np.full(3, 2) + np.add(A,B,out=B) np.divide(A,2,out=A) np.negative(A,out=A) np.multiply(A,B,out=A) ``` -#### 36. Extract the integer part of a random array using 5 different methods (★★☆) -`hint: %, np.floor, np.ceil, astype, np.trunc` +#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆) +`hint: %, np.floor, astype, np.trunc` ```python Z = np.random.uniform(0,10,10) print (Z - Z%1) print (np.floor(Z)) -print (np.ceil(Z)-1) print (Z.astype(int)) print (np.trunc(Z)) ``` @@ -409,12 +409,12 @@ Z.flags.writeable = False Z[0] = 1 ``` #### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆) -`hint: np.sqrt, np.arctan2` +`hint: np.hypot, np.arctan2` ```python Z = np.random.random((10,2)) X,Y = Z[:,0], Z[:,1] -R = np.sqrt(X**2+Y**2) +R = np.hypot(Y,X) T = np.arctan2(Y,X) print(R) print(T) @@ -493,7 +493,7 @@ print(Z) ```python Z = np.random.random((10,2)) X,Y = np.atleast_2d(Z[:,0], Z[:,1]) -D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2) +D = np.hypot(X-X.T, Y-Y.T) print(D) # Much faster with scipy @@ -552,7 +552,7 @@ for index in np.ndindex(Z.shape): ```python X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) -D = np.sqrt(X*X+Y*Y) +D = np.hypot(X, Y) sigma, mu = 1.0, 0.0 G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) print(G) @@ -673,10 +673,10 @@ print(F) # Author: Nadav Horesh w,h = 16,16 -I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) +I = np.random.randint(0,2,(h,w,3)) F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] n = len(np.unique(F)) -print(np.unique(I)) +print(n) ``` #### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) `hint: sum(axis=(-2,-1))` @@ -826,9 +826,9 @@ np.negative(Z, out=Z) def distance(P0, P1, p): T = P1 - P0 L = (T**2).sum(axis=1) - U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L + U = ((P0 - p)*T).sum(axis=1) / L U = U.reshape(len(U),1) - D = P0 + U*T - p + D = P0 - U*T - p return np.sqrt((D**2).sum(axis=1)) P0 = np.random.uniform(-10,10,(10,2)) diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index aaafa807..86ad9293 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -72,8 +72,8 @@ print(Z) ```python -nz = np.nonzero([1,2,0,0,4,0]) -print(nz) +Z = np.arange(9).reshape((3, 3)) +print(Z) ``` #### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆) @@ -151,7 +151,7 @@ print(0.3 == 3 * 0.1) ```python -Z = np.diag(1+np.arange(4),k=-1) +Z = np.diag(np.arange(1,5),k=-1) print(Z) ``` #### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆) @@ -211,7 +211,7 @@ print(Z) # Author: Evgeni Burovski Z = np.arange(11) -Z[(3 < Z) & (Z < 8)] *= -1 +Z[(3 < Z) & (Z <= 8)] *= -1 print(Z) ``` #### 26. What is the output of the following script? (★☆☆) @@ -292,8 +292,9 @@ Z = np.ones(1) / 0 _ = np.seterr(**defaults) # Equivalently with a context manager -nz = np.nonzero([1,2,0,0,4,0]) -print(nz) +with np.errstate(all='ignore'): + Z = np.arange(3) / 0 +print(Z) ``` #### 32. Is the following expressions true? (★☆☆) ```python @@ -323,15 +324,15 @@ print(Z) ```python -A = np.ones(3)*1 -B = np.ones(3)*2 -C = np.ones(3)*3 +A = np.ones(3) +B = np.full(3, 2) + np.add(A,B,out=B) np.divide(A,2,out=A) np.negative(A,out=A) np.multiply(A,B,out=A) ``` -#### 36. Extract the integer part of a random array using 5 different methods (★★☆) +#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆) ```python @@ -339,7 +340,6 @@ Z = np.random.uniform(0,10,10) print (Z - Z%1) print (np.floor(Z)) -print (np.ceil(Z)-1) print (Z.astype(int)) print (np.trunc(Z)) ``` @@ -414,7 +414,7 @@ Z[0] = 1 ```python Z = np.random.random((10,2)) X,Y = Z[:,0], Z[:,1] -R = np.sqrt(X**2+Y**2) +R = np.hypot(Y,X) T = np.arctan2(Y,X) print(R) print(T) @@ -493,7 +493,7 @@ print(Z) ```python Z = np.random.random((10,2)) X,Y = np.atleast_2d(Z[:,0], Z[:,1]) -D = np.sqrt( (X-X.T)**2 + (Y-Y.T)**2) +D = np.hypot(X-X.T, Y-Y.T) print(D) # Much faster with scipy @@ -552,7 +552,7 @@ for index in np.ndindex(Z.shape): ```python X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) -D = np.sqrt(X*X+Y*Y) +D = np.hypot(X, Y) sigma, mu = 1.0, 0.0 G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) print(G) @@ -673,10 +673,10 @@ print(F) # Author: Nadav Horesh w,h = 16,16 -I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte) +I = np.random.randint(0,2,(h,w,3)) F = I[...,0]*256*256 + I[...,1]*256 +I[...,2] n = len(np.unique(F)) -print(np.unique(I)) +print(n) ``` #### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★) @@ -826,9 +826,9 @@ np.negative(Z, out=Z) def distance(P0, P1, p): T = P1 - P0 L = (T**2).sum(axis=1) - U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L + U = ((P0 - p)*T).sum(axis=1) / L U = U.reshape(len(U),1) - D = P0 + U*T - p + D = P0 - U*T - p return np.sqrt((D**2).sum(axis=1)) P0 = np.random.uniform(-10,10,(10,2))