From 05346973f32b1f185cf829a658feb07c9dd11a30 Mon Sep 17 00:00:00 2001 From: Scott Archer-Nicholls Date: Tue, 21 Apr 2026 15:07:50 +0100 Subject: [PATCH 1/6] fix: broke up calculation of gaussian into 3 lines --- episodes/03-numpy_essential.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/episodes/03-numpy_essential.md b/episodes/03-numpy_essential.md index f07fc1b..d0f0463 100644 --- a/episodes/03-numpy_essential.md +++ b/episodes/03-numpy_essential.md @@ -194,8 +194,9 @@ Reminder: the Gaussian function is defined by: ```python def gauss(x, mu=0, sigma=1): - return (1. / (np.sqrt(2 * np.pi * sigma ** 2)) * - np.exp( - (x - mu) ** 2 / (2 * sigma ** 2))) + amplitude = 1. / (np.sqrt(2 * np.pi * sigma ** 2)) + exponent = - (x - mu) ** 2 / (2 * sigma ** 2) + return amplitude * np.exp(exponent) ``` ::::::::::::::::::::::::: From 047a74147ce4f3c6b4f11579902c81874d75b0c9 Mon Sep 17 00:00:00 2001 From: Scott Archer-Nicholls Date: Tue, 21 Apr 2026 15:26:31 +0100 Subject: [PATCH 2/6] fix: simplified the calculation, should also run faster as a good example --- episodes/03-numpy_essential.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/episodes/03-numpy_essential.md b/episodes/03-numpy_essential.md index d0f0463..9675332 100644 --- a/episodes/03-numpy_essential.md +++ b/episodes/03-numpy_essential.md @@ -194,9 +194,9 @@ Reminder: the Gaussian function is defined by: ```python def gauss(x, mu=0, sigma=1): - amplitude = 1. / (np.sqrt(2 * np.pi * sigma ** 2)) - exponent = - (x - mu) ** 2 / (2 * sigma ** 2) - return amplitude * np.exp(exponent) + amp = 1. / (np.sqrt(2 * np.pi) * sigma) + inv = 1. / (2. * np.square(sigma)) + return amp * np.exp(- np.square(x - mu) * inv) ``` ::::::::::::::::::::::::: From 12c664e4228fb7971c1f3e209eac00a235bc2929 Mon Sep 17 00:00:00 2001 From: Scott Archer-Nicholls Date: Tue, 21 Apr 2026 15:48:33 +0100 Subject: [PATCH 3/6] tidied up help text for challenge --- episodes/03-numpy_essential.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/episodes/03-numpy_essential.md b/episodes/03-numpy_essential.md index 9675332..d4445e0 100644 --- a/episodes/03-numpy_essential.md +++ b/episodes/03-numpy_essential.md @@ -170,8 +170,10 @@ Masked arrays associate a NumPy array with another array composed only of boolea To demonstrate this we are going to create a Gaussian function and use it to generate an example dataset and generate a plot. We will then add some noise to it and use a masked array to filter out the noisy data. This represents the kind of processing that can be used for datasets such as a seismographs, where we would wish to isolate single events from noisy background data. -Reminder: the Gaussian function is defined by: +*Reminder*: the Gaussian function is defined by: ![](fig/gauss_function.png){alt='Gaussian function equation.'} +where **x** is an array, **µ** is the position of the centre of the curve/peak and **σ** is the width of the bell. + ::::::::::::::::::::::::::::::::::::::: challenge @@ -179,14 +181,13 @@ Reminder: the Gaussian function is defined by: 1. Create a function called `gauss` which will take three arguments (inputs): **x**, **µ**, and **σ**, - as defined above. (x is an array, µ is the position of the centre of the - curve/peak and σ is the width of the bell) -2. Create a NumPy array using the 'numpy' function 'linspace' which will contain 1000 - points equally spaced between x=-100 and x=100. Hint: You can print the help - documentation of a function with 'help(name\_of\_the\_function)' -3. Using the above gauss function and the array, create a list which contains the value - of the gauss from x=-100 to x=100. -4. Use the 'matplotlib' library to plot the curve with mu=0 and sigma=10. + as defined above. *Hint*: you may wish to use the NumPy functions `square` and `sqrt` for calcuating the square and square-root respectively of all elements in an array. +2. Create a NumPy array using the Numpy function `linspace` which will contain 1000 + points equally spaced between **x**=-100 and **x**=100. *Hint*: You can print the help + documentation of a function with 'help(name\_of\_the\_function)'. +3. Using the above gauss function and the array, create an array which contains the values + of the Gaussian from **x**=-100 to **x**=100. +4. Use the 'matplotlib' library to plot the curve with **µ**=0 and **σ**=10. ::::::::::::::: solution From e853f504503297c697be9561e5cc5823ec430c98 Mon Sep 17 00:00:00 2001 From: Scott Archer-Nicholls Date: Tue, 21 Apr 2026 15:51:14 +0100 Subject: [PATCH 4/6] Also added help text for np.pi --- episodes/03-numpy_essential.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/episodes/03-numpy_essential.md b/episodes/03-numpy_essential.md index d4445e0..938e551 100644 --- a/episodes/03-numpy_essential.md +++ b/episodes/03-numpy_essential.md @@ -181,7 +181,7 @@ where **x** is an array, **µ** is the position of the centre of the curve/peak 1. Create a function called `gauss` which will take three arguments (inputs): **x**, **µ**, and **σ**, - as defined above. *Hint*: you may wish to use the NumPy functions `square` and `sqrt` for calcuating the square and square-root respectively of all elements in an array. + as defined above. *Hint*: you may wish to use the NumPy constant `pi`, and NumPy functions `square` and `sqrt` for calcuating the square and square-root respectively of all elements in an array. 2. Create a NumPy array using the Numpy function `linspace` which will contain 1000 points equally spaced between **x**=-100 and **x**=100. *Hint*: You can print the help documentation of a function with 'help(name\_of\_the\_function)'. From 5d87a4280218bb603510d8c89c009e4dbc675133 Mon Sep 17 00:00:00 2001 From: Scott Archer-Nicholls Date: Tue, 21 Apr 2026 15:53:47 +0100 Subject: [PATCH 5/6] also added help for exp function --- episodes/03-numpy_essential.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/episodes/03-numpy_essential.md b/episodes/03-numpy_essential.md index 938e551..1546558 100644 --- a/episodes/03-numpy_essential.md +++ b/episodes/03-numpy_essential.md @@ -181,7 +181,7 @@ where **x** is an array, **µ** is the position of the centre of the curve/peak 1. Create a function called `gauss` which will take three arguments (inputs): **x**, **µ**, and **σ**, - as defined above. *Hint*: you may wish to use the NumPy constant `pi`, and NumPy functions `square` and `sqrt` for calcuating the square and square-root respectively of all elements in an array. + as defined above. *Hint*: you may wish to use the NumPy constant `pi`, and NumPy functions `square`, `sqrt` and `exp` for calcuating the square, square-root and exponential of **e** respectively for all elements in an array. 2. Create a NumPy array using the Numpy function `linspace` which will contain 1000 points equally spaced between **x**=-100 and **x**=100. *Hint*: You can print the help documentation of a function with 'help(name\_of\_the\_function)'. From 6eb6903ceb780bb6772f05b7760bde0aaeb7c372 Mon Sep 17 00:00:00 2001 From: Scott Archer-Nicholls Date: Tue, 21 Apr 2026 15:57:48 +0100 Subject: [PATCH 6/6] added space after Gaussian function image --- episodes/03-numpy_essential.md | 1 + 1 file changed, 1 insertion(+) diff --git a/episodes/03-numpy_essential.md b/episodes/03-numpy_essential.md index 1546558..a1f90d5 100644 --- a/episodes/03-numpy_essential.md +++ b/episodes/03-numpy_essential.md @@ -172,6 +172,7 @@ To demonstrate this we are going to create a Gaussian function and use it to gen *Reminder*: the Gaussian function is defined by: ![](fig/gauss_function.png){alt='Gaussian function equation.'} + where **x** is an array, **µ** is the position of the centre of the curve/peak and **σ** is the width of the bell.