Skip to content

Commit 8fe181a

Browse files
committed
Incorporate feedback
1 parent 2762df0 commit 8fe181a

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed

core/numpy/intermediate-numpy.ipynb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"source": [
5959
"## Using axes to slice arrays\n",
6060
"\n",
61-
"The solution to the last exercise in the Numpy Basics notebook introduces an important concept when working with NumPy: the axis. This indicates the particular dimension along which a function should operate (provided the function does something taking multiple values and converts to a single value). \n",
61+
"Here we introduce an important concept when working with NumPy: the axis. This indicates the particular dimension along which a function should operate (provided the function does something taking multiple values and converts to a single value). \n",
6262
"\n",
6363
"Let's look at a concrete example with `sum`:"
6464
]
@@ -234,7 +234,7 @@
234234
"cell_type": "markdown",
235235
"metadata": {},
236236
"source": [
237-
"Finally, we can calculate the dot product of these two multidimensional fields of wind and temperature gradient components by hand as an element-wise multiplication, `*`, and then a `sum` of our separate components at each point,"
237+
"Finally, we can calculate the dot product of these two multidimensional fields of wind and temperature gradient components by hand as an element-wise multiplication, `*`, and then a `sum` of our separate components at each point (i.e., along the last `axis`),"
238238
]
239239
},
240240
{
@@ -252,6 +252,8 @@
252252
"metadata": {},
253253
"source": [
254254
"## Indexing arrays with boolean values\n",
255+
"\n",
256+
"### Array comparisons\n",
255257
"NumPy can easily create arrays of boolean values and use those to select certain values to extract from an array"
256258
]
257259
},
@@ -318,7 +320,7 @@
318320
"metadata": {},
319321
"source": [
320322
"<div class=\"admonition alert alert-info\">\n",
321-
" <p class=\"title\" style=\"font-weight:bold\">Info</p>\n",
323+
" <p class=\"admonition-title\" style=\"font-weight:bold\">Info</p>\n",
322324
" This only returns the values from our original array meeting the indexing conditions, nothing more! Note the size,\n",
323325
"</div>"
324326
]
@@ -407,6 +409,7 @@
407409
"cell_type": "markdown",
408410
"metadata": {},
409411
"source": [
412+
"### Replacing values\n",
410413
"To extend this, we can use this conditional indexing to _assign_ new values to certain positions within our array, somewhat like a masking operation."
411414
]
412415
},

core/numpy/numpy-basics.ipynb

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@
374374
"metadata": {},
375375
"source": [
376376
"<div class=\"admonition alert alert-warning\">\n",
377-
" <p class=\"warning\" style=\"font-weight:bold\">Warning</p>\n",
377+
" <p class=\"admonition-title\" style=\"font-weight:bold\">Warning</p>\n",
378378
" These arrays must be the same shape!\n",
379379
"</div>"
380380
]
@@ -408,7 +408,7 @@
408408
"source": [
409409
"### Constants\n",
410410
"\n",
411-
"NumPy proves us access to some useful constants as well - remember you should never be typing these in manually! Other libraries such as SciPy and MetPy have their own set of constants that are more domain specific."
411+
"NumPy provides us access to some useful constants as well - remember you should never be typing these in manually! Other libraries such as SciPy and MetPy have their own set of constants that are more domain specific."
412412
]
413413
},
414414
{
@@ -474,6 +474,13 @@
474474
"sin_t"
475475
]
476476
},
477+
{
478+
"cell_type": "markdown",
479+
"metadata": {},
480+
"source": [
481+
"and clean it up a bit by `round`ing to three decimal places."
482+
]
483+
},
477484
{
478485
"cell_type": "code",
479486
"execution_count": null,
@@ -498,7 +505,7 @@
498505
"metadata": {},
499506
"source": [
500507
"<div class=\"admonition alert alert-info\">\n",
501-
" <p class=\"title\" style=\"font-weight:bold\">Info</p>\n",
508+
" <p class=\"admonition-title\" style=\"font-weight:bold\">Info</p>\n",
502509
" Check out NumPy's list of mathematical functions <a href=https://numpy.org/doc/stable/reference/routines.math.html>here</a>!\n",
503510
"</div>"
504511
]
@@ -783,7 +790,7 @@
783790
"metadata": {},
784791
"source": [
785792
"<div class=\"admonition alert alert-warning\">\n",
786-
" <p class=\"warning\" style=\"font-weight:bold\">Warning</p>\n",
793+
" <p class=\"admonition-title\" style=\"font-weight:bold\">Warning</p>\n",
787794
" Slice notation is <b>exclusive</b> of the final index.\n",
788795
"</div>"
789796
]
@@ -792,7 +799,7 @@
792799
"cell_type": "markdown",
793800
"metadata": {},
794801
"source": [
795-
"This means that slices will include ever value **up to** your `stop` index and not this index itself, like a half-open interval `[start, end)`. For example,"
802+
"This means that slices will include every value **up to** your `stop` index and not this index itself, like a half-open interval `[start, end)`. For example,"
796803
]
797804
},
798805
{
@@ -849,6 +856,7 @@
849856
"cell_type": "markdown",
850857
"metadata": {},
851858
"source": [
859+
"### Multidimensional slicing\n",
852860
"This entire syntax can be extended to each dimension of multidimensional arrays."
853861
]
854862
},
@@ -861,6 +869,13 @@
861869
"a"
862870
]
863871
},
872+
{
873+
"cell_type": "markdown",
874+
"metadata": {},
875+
"source": [
876+
"First let's pull out rows `0` through `2`, and then every `:` column for each of those"
877+
]
878+
},
864879
{
865880
"cell_type": "code",
866881
"execution_count": null,
@@ -870,6 +885,13 @@
870885
"a[0:2, :]"
871886
]
872887
},
888+
{
889+
"cell_type": "markdown",
890+
"metadata": {},
891+
"source": [
892+
"Similarly, let's get all rows for just column `2`,"
893+
]
894+
},
873895
{
874896
"cell_type": "code",
875897
"execution_count": null,
@@ -879,6 +901,13 @@
879901
"a[:, 2]"
880902
]
881903
},
904+
{
905+
"cell_type": "markdown",
906+
"metadata": {},
907+
"source": [
908+
"or just take a look at the full row `:`, for every second column, `::2`,"
909+
]
910+
},
882911
{
883912
"cell_type": "code",
884913
"execution_count": null,
@@ -892,7 +921,7 @@
892921
"cell_type": "markdown",
893922
"metadata": {},
894923
"source": [
895-
"For any shape of array, you can use `...` to capture full slices of every un-specific dimension."
924+
"For any shape of array, you can use `...` to capture full slices of every non-specified dimension. Consider the 3-D array,"
896925
]
897926
},
898927
{
@@ -914,6 +943,29 @@
914943
"c[0, ...]"
915944
]
916945
},
946+
{
947+
"cell_type": "markdown",
948+
"metadata": {},
949+
"source": [
950+
"and so this is equivalent to"
951+
]
952+
},
953+
{
954+
"cell_type": "code",
955+
"execution_count": null,
956+
"metadata": {},
957+
"outputs": [],
958+
"source": [
959+
"c[0, :, :]"
960+
]
961+
},
962+
{
963+
"cell_type": "markdown",
964+
"metadata": {},
965+
"source": [
966+
"for extracting every dimension across our first row. We can also flip this around,"
967+
]
968+
},
917969
{
918970
"cell_type": "code",
919971
"execution_count": null,
@@ -923,6 +975,13 @@
923975
"c[..., -1]"
924976
]
925977
},
978+
{
979+
"cell_type": "markdown",
980+
"metadata": {},
981+
"source": [
982+
"to investigate every preceding dimension along our the last entry of our last axis, the same as `c[:, :, -1]`."
983+
]
984+
},
926985
{
927986
"cell_type": "markdown",
928987
"metadata": {},
@@ -968,7 +1027,8 @@
9681027
"nbconvert_exporter": "python",
9691028
"pygments_lexer": "ipython3",
9701029
"version": "3.8.10"
971-
}
1030+
},
1031+
"toc-autonumbering": false
9721032
},
9731033
"nbformat": 4,
9741034
"nbformat_minor": 4

core/numpy/numpy-broadcasting.ipynb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"cell_type": "markdown",
6565
"metadata": {},
6666
"source": [
67+
"### What is broadcasting?\n",
6768
"Broadcasting is a useful NumPy tool that allows us to perform operations between arrays with different shapes, provided that they are compatible with each other in certain ways. To start, we can create an array below and add 5 to it:"
6869
]
6970
},
@@ -204,6 +205,7 @@
204205
"cell_type": "markdown",
205206
"metadata": {},
206207
"source": [
208+
"### Giving NumPy room for broadcasting\n",
207209
"We can also do this using broadcasting, which is where NumPy implicitly repeats the array without using additional memory. With broadcasting, NumPy takes care of repeating for you, provided dimensions are \"compatible\". This works as:\n",
208210
"1. Check the number of dimensions of the arrays. If they are different, *prepend* size one dimensions\n",
209211
"2. Check if each of the dimensions are compatible: either the same size, or one of them is 1."
@@ -282,7 +284,8 @@
282284
"cell_type": "markdown",
283285
"metadata": {},
284286
"source": [
285-
"This also works for higher dimensional broadcasting,"
287+
"### Extending to higher dimensions\n",
288+
"This also works for higher dimensions. `x`, `y`, and `z` are here different dimensions, and we can broadcast to perform $x^2 + y^2 + z^2$,"
286289
]
287290
},
288291
{
@@ -296,6 +299,13 @@
296299
"z = np.array([6, 7, 8, 9])"
297300
]
298301
},
302+
{
303+
"cell_type": "markdown",
304+
"metadata": {},
305+
"source": [
306+
"First, let's extend `x` (and square it) by one dimension, onto which we can broadcast the vector `y ** 2`,"
307+
]
308+
},
299309
{
300310
"cell_type": "code",
301311
"execution_count": null,
@@ -314,6 +324,13 @@
314324
"d_2d.shape"
315325
]
316326
},
327+
{
328+
"cell_type": "markdown",
329+
"metadata": {},
330+
"source": [
331+
"and then further extend this new 2-D array by one more dimension before using broadcasting to add `z ** 2` across all other dimensions."
332+
]
333+
},
317334
{
318335
"cell_type": "code",
319336
"execution_count": null,
@@ -594,7 +611,7 @@
594611
"cell_type": "markdown",
595612
"metadata": {},
596613
"source": [
597-
"Let's start by writing a loop to take a 3-point running mean of the data. We'll do this by iterating over all the point in the array and average the 3 points centered on that point. We'll simplify the problem by avoiding dealing with the cases at the edges of the array."
614+
"Let's start by writing a loop to take a 3-point running mean of the data. We'll do this by iterating over all points in the array and average the 3 points centered on that point. We'll simplify the problem by avoiding dealing with the cases at the edges of the array."
598615
]
599616
},
600617
{

0 commit comments

Comments
 (0)