|
19 | 19 | "cell_type": "markdown",
|
20 | 20 | "metadata": {},
|
21 | 21 | "source": [
|
22 |
| - "The efficient storage and manipulation of numerical arrays is often critical when processing ocean data. This notebook will present a specialized Python package for handling such numerical arrays: [NumPy](https://www.numpy.org/) ." |
| 22 | + "The efficient storage and manipulation of numerical arrays is often critical when processing ocean data. This notebook will present a specialized Python package for handling such numerical arrays: [NumPy](https://www.numpy.org/)." |
23 | 23 | ]
|
24 | 24 | },
|
25 | 25 | {
|
|
42 | 42 | "cell_type": "markdown",
|
43 | 43 | "metadata": {},
|
44 | 44 | "source": [
|
45 |
| - "NumPy (short for Numerical Python) is centered around a powerful N-dimensional array object, but also contains other useful capabilities like linear algebra, Fourier transform, functions to create random numbers, etc." |
| 45 | + "NumPy (short for Numerical Python) is centered around a powerful N-dimensional array object, but also provides other useful capabilities like linear algebra, Fourier transform, functions to create random numbers, etc." |
46 | 46 | ]
|
47 | 47 | },
|
48 | 48 | {
|
|
58 | 58 | "cell_type": "markdown",
|
59 | 59 | "metadata": {},
|
60 | 60 | "source": [
|
61 |
| - "Before starting to use `numpy`, you have to execute the following cell that, together with code introduced in the past notebooks, imports `numpy` with the commonly-adopted short name of `np`:" |
| 61 | + "Before starting to use `numpy`, you have to execute the following cell. Together with code introduced in the past notebooks, this imports `numpy` and assign it the commonly-adopted short name of `np`:" |
62 | 62 | ]
|
63 | 63 | },
|
64 | 64 | {
|
|
106 | 106 | "cell_type": "markdown",
|
107 | 107 | "metadata": {},
|
108 | 108 | "source": [
|
109 |
| - "## Creating an Array with Zero Values" |
| 109 | + "## Creating an Array Filled with Zeros " |
110 | 110 | ]
|
111 | 111 | },
|
112 | 112 | {
|
113 | 113 | "cell_type": "markdown",
|
114 | 114 | "metadata": {},
|
115 | 115 | "source": [
|
116 |
| - "The first `numpy` function that we introduce is [`zeros()`](https://www.numpy.org/devdocs/reference/generated/numpy.zeros.html?#numpy.zeros). When a single integer value is passed, this function creates an 1D array containing the passed number of zeros:" |
| 116 | + "The first `numpy` function that we introduce is [`zeros()`](https://www.numpy.org/devdocs/reference/generated/numpy.zeros.html?#numpy.zeros). When a single integer is passed, this function creates an 1D array with a number of zero equal to the passed value:" |
117 | 117 | ]
|
118 | 118 | },
|
119 | 119 | {
|
|
130 | 130 | "cell_type": "markdown",
|
131 | 131 | "metadata": {},
|
132 | 132 | "source": [
|
133 |
| - "It is also possible to create multi-dimensional array by passing a [`tuple`](https://docs.python.org/3.6/library/stdtypes.html?#tuples).\n", |
| 133 | + "It is also possible to create multi-dimensional array by passing a [`tuple`](https://docs.python.org/3.6/library/stdtypes.html?#tuples). Each value in the `tuple` define the array size for each dimension.\n", |
134 | 134 | "\n",
|
135 |
| - "Thus, the following code creates a 2D array with 3 rows and 8 columns passing `(3, 8)`:" |
| 135 | + "Thus, the following code creates a 2D array with 8 rows and 2 columns passing `(8, 2)`:" |
136 | 136 | ]
|
137 | 137 | },
|
138 | 138 | {
|
|
141 | 141 | "metadata": {},
|
142 | 142 | "outputs": [],
|
143 | 143 | "source": [
|
144 |
| - "arr = np.zeros((3, 8))\n", |
| 144 | + "arr = np.zeros((8, 2))\n", |
145 | 145 | "print(arr)"
|
146 | 146 | ]
|
147 | 147 | },
|
|
216 | 216 | "print(\"Array dimensions: %d\" % arr.ndim)"
|
217 | 217 | ]
|
218 | 218 | },
|
| 219 | + { |
| 220 | + "cell_type": "markdown", |
| 221 | + "metadata": {}, |
| 222 | + "source": [ |
| 223 | + "The number of elements along each of the axes is the **length** of the axes." |
| 224 | + ] |
| 225 | + }, |
219 | 226 | {
|
220 | 227 | "cell_type": "markdown",
|
221 | 228 | "metadata": {},
|
|
354 | 361 | "cell_type": "markdown",
|
355 | 362 | "metadata": {},
|
356 | 363 | "source": [
|
357 |
| - "The `zeros()` is just one of the several functions available to create an array in `numpy`. \n", |
| 364 | + "The `zeros()` function is just one of the several ways to create an array in `numpy`. \n", |
358 | 365 | "\n",
|
359 | 366 | "For instance, to create an array of a given shape, but initialized with a specific value (e.g., `8.0`), you can use the [`full()`](https://www.numpy.org/devdocs/reference/generated/numpy.full.html) function:"
|
360 | 367 | ]
|
|
373 | 380 | "cell_type": "markdown",
|
374 | 381 | "metadata": {},
|
375 | 382 | "source": [
|
376 |
| - "If you want to create an array filled with a linear sequence of values, you may use the [`arange()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html) function or the [`linspace()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html) function by selecting the one that fulfills your specific needs:" |
| 383 | + "If you want to create an array filled with a sequence of values, you may use the [`arange()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html) function or the [`linspace()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html) function by selecting the one that fulfills your specific needs:" |
377 | 384 | ]
|
378 | 385 | },
|
379 | 386 | {
|
|
382 | 389 | "metadata": {},
|
383 | 390 | "outputs": [],
|
384 | 391 | "source": [
|
385 |
| - "arr = np.arange(start=0, stop=40, step=8) # arange excludes the `stop` value\n", |
| 392 | + "arr = np.arange(start=0, stop=40, step=8) # the `stop` value is not included\n", |
386 | 393 | "print(arr)"
|
387 | 394 | ]
|
388 | 395 | },
|
|
392 | 399 | "metadata": {},
|
393 | 400 | "outputs": [],
|
394 | 401 | "source": [
|
395 |
| - "arr = np.linspace(start=0, stop=40, num=5)\n", |
| 402 | + "arr = np.linspace(start=0, stop=40, num=5) # the `stop` value is included\n", |
396 | 403 | "print(arr)"
|
397 | 404 | ]
|
398 | 405 | },
|
|
501 | 508 | "cell_type": "markdown",
|
502 | 509 | "metadata": {},
|
503 | 510 | "source": [
|
504 |
| - "One of the advantages of using `numpy` arrays in place of `list` containers is the many mathematical and statistical methods available in NumPy." |
| 511 | + "Numpy arrays have many efficient mathematical and statistical methods. This is one of the motivations to use them in place of `list` containers.\n", |
| 512 | + "\n", |
| 513 | + "The cell below shows how to use some of these methods. If you want to learn more about them, visit [this Numpy page](https://www.numpy.org/devdocs/reference/arrays.ndarray.html#calculation)." |
505 | 514 | ]
|
506 | 515 | },
|
507 | 516 | {
|
|
550 | 559 | "temp_list = [11.2, 11.0, 13.7, 16.0, 16.1, 16.2, 16.1]\n",
|
551 | 560 | "\n",
|
552 | 561 | "temp_arr = np.array(temp_list)\n",
|
553 |
| - "avg_temp = temp_arr.mean()\n", |
| 562 | + "mean_temp = temp_arr.mean()\n", |
554 | 563 | "min_temp = temp_arr.min()\n",
|
555 | 564 | "max_temp = temp_arr.max()\n",
|
556 | 565 | "print(\"The average value is %f\" % (mean_temp, ))\n",
|
|
559 | 568 | "\n",
|
560 | 569 | "# plot creation\n",
|
561 | 570 | "plt.plot(temp_arr, color='orange', marker='o', label='data')\n",
|
562 |
| - "plt.axhline(y=avg_temp, color='green', linestyle='dashed', label='avg')\n", |
| 571 | + "plt.axhline(y=mean_temp, color='green', linestyle='dashed', label='avg')\n", |
563 | 572 | "plt.axhline(y=min_temp, color='blue', linestyle='dotted', label='min')\n",
|
564 | 573 | "plt.axhline(y=max_temp, color='red', linestyle='dotted', label='max')\n",
|
565 | 574 | "plt.title(\"Temperature\") \n",
|
|
910 | 919 | "***"
|
911 | 920 | ]
|
912 | 921 | },
|
913 |
| - { |
914 |
| - "cell_type": "markdown", |
915 |
| - "metadata": {}, |
916 |
| - "source": [ |
917 |
| - "## Array Views and Copies" |
918 |
| - ] |
919 |
| - }, |
920 |
| - { |
921 |
| - "cell_type": "markdown", |
922 |
| - "metadata": {}, |
923 |
| - "source": [ |
924 |
| - "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
925 |
| - "\n", |
926 |
| - "Array slicing returns views rather than copies of the array data." |
927 |
| - ] |
928 |
| - }, |
929 |
| - { |
930 |
| - "cell_type": "markdown", |
931 |
| - "metadata": {}, |
932 |
| - "source": [ |
933 |
| - "The relevant implication is that, if you modify a sub-array, the original array is modified too!" |
934 |
| - ] |
935 |
| - }, |
936 |
| - { |
937 |
| - "cell_type": "markdown", |
938 |
| - "metadata": {}, |
939 |
| - "source": [ |
940 |
| - "If we extract the temperature row as a sub-array and modify it (by assigning a common value), the original `data_arr` will change too:" |
941 |
| - ] |
942 |
| - }, |
943 |
| - { |
944 |
| - "cell_type": "code", |
945 |
| - "execution_count": null, |
946 |
| - "metadata": {}, |
947 |
| - "outputs": [], |
948 |
| - "source": [ |
949 |
| - "# Create a two-dimensional data array\n", |
950 |
| - "sal_list = [34.4, 34.1, 33.6, 31.7, 31.3, 31.2, 31.0]\n", |
951 |
| - "temp_list = [11.2, 11.0, 13.7, 16.0, 16.1, 16.2, 16.1]\n", |
952 |
| - "data_arr = np.array([sal_list, temp_list])\n", |
953 |
| - "print(\"data array before:\")\n", |
954 |
| - "print(data_arr)\n", |
955 |
| - "\n", |
956 |
| - "# Slice and modify the resulting sub-array\n", |
957 |
| - "temp_arr = data_arr[1, :]\n", |
958 |
| - "temp_arr[0] = -9999.9\n", |
959 |
| - "print(\"temperature sub-array:\")\n", |
960 |
| - "print(temp_arr)\n", |
961 |
| - "print(\"data array after:\")\n", |
962 |
| - "print(data_arr)" |
963 |
| - ] |
964 |
| - }, |
965 |
| - { |
966 |
| - "cell_type": "markdown", |
967 |
| - "metadata": {}, |
968 |
| - "source": [ |
969 |
| - "This default behavior is quite useful when you handle large array since you can access sub-arrays without copying the underlying data." |
970 |
| - ] |
971 |
| - }, |
972 |
| - { |
973 |
| - "cell_type": "markdown", |
974 |
| - "metadata": {}, |
975 |
| - "source": [ |
976 |
| - "If you need a copy of a specific array or sub-array, you can explicitly copy the data using the [`copy()`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.copy.html) method:" |
977 |
| - ] |
978 |
| - }, |
979 |
| - { |
980 |
| - "cell_type": "code", |
981 |
| - "execution_count": null, |
982 |
| - "metadata": {}, |
983 |
| - "outputs": [], |
984 |
| - "source": [ |
985 |
| - "# Create a two-dimensional data array\n", |
986 |
| - "sal_list = [34.4, 34.1, 33.6, 31.7, 31.3, 31.2, 31.0]\n", |
987 |
| - "temp_list = [11.2, 11.0, 13.7, 16.0, 16.1, 16.2, 16.1]\n", |
988 |
| - "data_arr = np.array([sal_list, temp_list])\n", |
989 |
| - "print(\"before:\")\n", |
990 |
| - "print(data_arr)\n", |
991 |
| - "\n", |
992 |
| - "# Slice, MAKE A COPY, and modify the resulting sub-array\n", |
993 |
| - "temp_arr = data_arr[1, :].copy()\n", |
994 |
| - "temp_arr[0] = -9999.9\n", |
995 |
| - "print(\"temperature sub-array:\")\n", |
996 |
| - "print(temp_arr)\n", |
997 |
| - "print(\"after:\")\n", |
998 |
| - "print(data_arr)" |
999 |
| - ] |
1000 |
| - }, |
1001 |
| - { |
1002 |
| - "cell_type": "markdown", |
1003 |
| - "metadata": {}, |
1004 |
| - "source": [ |
1005 |
| - "---" |
1006 |
| - ] |
1007 |
| - }, |
1008 | 922 | {
|
1009 | 923 | "cell_type": "markdown",
|
1010 | 924 | "metadata": {},
|
|
0 commit comments