Skip to content

Commit ce49ed6

Browse files
committed
Drew's suggestions
1 parent 26af0f0 commit ce49ed6

1 file changed

Lines changed: 80 additions & 49 deletions

File tree

content/basics/numpy_arrays.ipynb

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
"\n",
2323
"| Experiment | Melting Point ($^{\\circ}$C) |\n",
2424
"| ------------- |:-------------:|\n",
25-
"| 1 | 99.5 $\\pm$ 0.1|\n",
25+
"| 1 | 98.5 $\\pm$ 0.1|\n",
2626
"| 2 | 99.9 $\\pm$ 0.1 |\n",
27-
"| 3 | 99.6 $\\pm$ 0.1 |\n",
27+
"| 3 | 100.6 $\\pm$ 0.1 |\n",
2828
"| 4 | 99.3 $\\pm$ 0.1 |\n",
29-
"| 5 | 99.7 $\\pm$ 0.1 |\n",
29+
"| 5 | 100.7 $\\pm$ 0.1 |\n",
3030
"| 6 | 99.4 $\\pm$ 0.1 |\n",
31-
"| 7 | 99.4 $\\pm$ 0.1 |\n",
31+
"| 7 | 98.4 $\\pm$ 0.1 |\n",
3232
"| 8 | 99.5 $\\pm$ 0.1 |\n",
3333
"| 9 | 99.3 $\\pm$ 0.1 |\n",
34-
"| 10 | 99.7 $\\pm$ 0.1 |\n"
34+
"| 10 | 100.7 $\\pm$ 0.1 |\n"
3535
]
3636
},
3737
{
@@ -41,13 +41,20 @@
4141
"### Importing NumPy & creating an array"
4242
]
4343
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"We want to store this information in a NumPy array so that we can calculate certain properties. Before we can use a NumPy array we must import the NumPy module as follows"
49+
]
50+
},
4451
{
4552
"cell_type": "code",
4653
"execution_count": 1,
4754
"metadata": {
4855
"ExecuteTime": {
49-
"end_time": "2020-05-03T17:23:26.266233Z",
50-
"start_time": "2020-05-03T17:23:26.052585Z"
56+
"end_time": "2020-05-04T17:48:21.986993Z",
57+
"start_time": "2020-05-04T17:48:21.752459Z"
5158
}
5259
},
5360
"outputs": [],
@@ -67,13 +74,13 @@
6774
"execution_count": 2,
6875
"metadata": {
6976
"ExecuteTime": {
70-
"end_time": "2020-05-03T17:23:26.271913Z",
71-
"start_time": "2020-05-03T17:23:26.268125Z"
77+
"end_time": "2020-05-04T17:48:21.995012Z",
78+
"start_time": "2020-05-04T17:48:21.989331Z"
7279
}
7380
},
7481
"outputs": [],
7582
"source": [
76-
"melting_point_data = numpy.array([99.5, 99.9, 99.6, 99.3, 99.7, 99.4, 99.4, 99.5, 99.3, 99.7])"
83+
"melting_point_data = numpy.array([98.5, 99.9, 100.6, 99.3, 100.7, 99.4, 98.4, 99.5, 99.3, 100.7])"
7784
]
7885
},
7986
{
@@ -90,8 +97,8 @@
9097
"execution_count": 3,
9198
"metadata": {
9299
"ExecuteTime": {
93-
"end_time": "2020-05-03T17:23:26.279590Z",
94-
"start_time": "2020-05-03T17:23:26.274561Z"
100+
"end_time": "2020-05-04T17:48:22.008428Z",
101+
"start_time": "2020-05-04T17:48:21.999481Z"
95102
}
96103
},
97104
"outputs": [
@@ -119,16 +126,16 @@
119126
"execution_count": 4,
120127
"metadata": {
121128
"ExecuteTime": {
122-
"end_time": "2020-05-03T17:23:26.291698Z",
123-
"start_time": "2020-05-03T17:23:26.283151Z"
129+
"end_time": "2020-05-04T17:48:22.024986Z",
130+
"start_time": "2020-05-04T17:48:22.014280Z"
124131
}
125132
},
126133
"outputs": [
127134
{
128135
"name": "stdout",
129136
"output_type": "stream",
130137
"text": [
131-
"[99.4 99.4 99.5 99.3 99.7]\n"
138+
"[ 99.4 98.4 99.5 99.3 100.7]\n"
132139
]
133140
}
134141
],
@@ -150,16 +157,16 @@
150157
"execution_count": 5,
151158
"metadata": {
152159
"ExecuteTime": {
153-
"end_time": "2020-05-03T17:23:26.303771Z",
154-
"start_time": "2020-05-03T17:23:26.295793Z"
160+
"end_time": "2020-05-04T17:48:22.036661Z",
161+
"start_time": "2020-05-04T17:48:22.029171Z"
155162
}
156163
},
157164
"outputs": [
158165
{
159166
"name": "stdout",
160167
"output_type": "stream",
161168
"text": [
162-
"[100. 100.4 100.1 99.8 100.2 99.9 99.9 100. 99.8 100.2]\n"
169+
"[ 99. 100.4 101.1 99.8 101.2 99.9 98.9 100. 99.8 101.2]\n"
163170
]
164171
}
165172
],
@@ -181,8 +188,8 @@
181188
"execution_count": 6,
182189
"metadata": {
183190
"ExecuteTime": {
184-
"end_time": "2020-05-03T17:23:26.313972Z",
185-
"start_time": "2020-05-03T17:23:26.308114Z"
191+
"end_time": "2020-05-04T17:48:22.047129Z",
192+
"start_time": "2020-05-04T17:48:22.039321Z"
186193
}
187194
},
188195
"outputs": [
@@ -191,7 +198,7 @@
191198
"output_type": "stream",
192199
"text": [
193200
"Subtracting 0.5 from the original melting point data results in:\n",
194-
"[99. 99.4 99.1 98.8 99.2 98.9 98.9 99. 98.8 99.2]\n"
201+
"[ 98. 99.4 100.1 98.8 100.2 98.9 97.9 99. 98.8 100.2]\n"
195202
]
196203
}
197204
],
@@ -207,8 +214,8 @@
207214
"execution_count": 7,
208215
"metadata": {
209216
"ExecuteTime": {
210-
"end_time": "2020-05-03T17:23:26.324863Z",
211-
"start_time": "2020-05-03T17:23:26.316791Z"
217+
"end_time": "2020-05-04T17:48:22.058298Z",
218+
"start_time": "2020-05-04T17:48:22.050874Z"
212219
}
213220
},
214221
"outputs": [
@@ -217,7 +224,7 @@
217224
"output_type": "stream",
218225
"text": [
219226
"Multiplying the original melting point data by two results in:\n",
220-
"[199. 199.8 199.2 198.6 199.4 198.8 198.8 199. 198.6 199.4]\n"
227+
"[197. 199.8 201.2 198.6 201.4 198.8 196.8 199. 198.6 201.4]\n"
221228
]
222229
}
223230
],
@@ -233,8 +240,8 @@
233240
"execution_count": 8,
234241
"metadata": {
235242
"ExecuteTime": {
236-
"end_time": "2020-05-03T17:23:26.336392Z",
237-
"start_time": "2020-05-03T17:23:26.329536Z"
243+
"end_time": "2020-05-04T17:48:22.075289Z",
244+
"start_time": "2020-05-04T17:48:22.066940Z"
238245
}
239246
},
240247
"outputs": [
@@ -243,7 +250,7 @@
243250
"output_type": "stream",
244251
"text": [
245252
"Dividing the original melting point data by 2 results in:\n",
246-
"[49.75 49.95 49.8 49.65 49.85 49.7 49.7 49.75 49.65 49.85]\n"
253+
"[49.25 49.95 50.3 49.65 50.35 49.7 49.2 49.75 49.65 50.35]\n"
247254
]
248255
}
249256
],
@@ -254,6 +261,30 @@
254261
"print(division_example)"
255262
]
256263
},
264+
{
265+
"cell_type": "markdown",
266+
"metadata": {},
267+
"source": [
268+
"### Speed up and datatypes\n",
269+
"\n",
270+
"Why is NumPy's broadcasting capabilities preferable to using lists? **NumPy arrays are significantly faster**. For example, if we were to take an array and a list of 100 values each, and we wished to enact the four simple operations above on every element in each collection then it would take the following times for python to complete these calculations. \n",
271+
"\n",
272+
"| Operation | Array | List | Speed up factor |\n",
273+
"| ------------- |:-------------:|:-------:|:------:\n",
274+
"| Addition | 9.92E-07 | 6.66E-6 | 6.7 |\n",
275+
"| Subtraction | 1.04E-6 | 9.73E-6 | 9.4 |\n",
276+
"| Multiplication | 1.03E-6 | 8.66E-6 | 8.4 |\n",
277+
"| Division | 1.11E-6 | 9.11E-6 | 8.2 |\n",
278+
"\n",
279+
"The use of NumPy arrays provides a significant speed up. This is just for one operation. When compounded throughout a code, this could be the difference between making a code feasible to run or not. \n",
280+
"\n",
281+
"This speed up occurs because an array is a simpler, less maleable collection than a list. As you have previously seen, a list can contain any type of data. Arrays are designed to only use one type of data. Every value in the array should be the same data type for maximum efficient ie all pieces of data should be floats.\n",
282+
"\n",
283+
"\n",
284+
"> #### A note on NumPy datatypes\n",
285+
"> It is possible to have different types of data in a NumPy array. **This is not recommended**. Using different datatypes in an NumPy array will, at best, significantly reduce the efficiency of your code or, at worst, may stop your code working completely. "
286+
]
287+
},
257288
{
258289
"cell_type": "markdown",
259290
"metadata": {},
@@ -268,17 +299,17 @@
268299
"execution_count": 9,
269300
"metadata": {
270301
"ExecuteTime": {
271-
"end_time": "2020-05-03T17:23:26.351301Z",
272-
"start_time": "2020-05-03T17:23:26.343358Z"
302+
"end_time": "2020-05-04T17:48:22.091657Z",
303+
"start_time": "2020-05-04T17:48:22.079804Z"
273304
}
274305
},
275306
"outputs": [
276307
{
277308
"name": "stdout",
278309
"output_type": "stream",
279310
"text": [
280-
"The mean of our data is 100.03 degrees Celsius.\n",
281-
"The median of our data is 100.0 degrees Celsius.\n"
311+
"The mean of our data is 100.13 degrees Celsius.\n",
312+
"The median of our data is 99.95 degrees Celsius.\n"
282313
]
283314
}
284315
],
@@ -294,7 +325,7 @@
294325
"cell_type": "markdown",
295326
"metadata": {},
296327
"source": [
297-
"It can be seen the mean and median melting points determined by our ten experiments are 100.03 $^{o}$C and 100.0 $^{o}$C respectively. To obtain the mean and median values we had to utilise the mean and median functions inside the numpy package. This was indicated by numpy.mean and numpy.median . The word before the dot is the package we wish to use, and the word after the dot is the function we wish to use. \n",
328+
"It can be seen the mean and median melting points determined by our ten experiments are 100.13 $^{o}$C and 99.95 $^{o}$C respectively. To obtain the mean and median values we had to utilise the mean and median functions inside the numpy package. This was indicated by numpy.mean and numpy.median . The word before the dot is the package we wish to use, and the word after the dot is the function we wish to use. \n",
298329
"\n",
299330
"NumPy is a very common package in python. It can become laborious writing numpy.function whenever we wish to utilise one of its functions. Programmers often look to be as efficient as possible with their time and use the following to reduce what they have to type"
300331
]
@@ -304,17 +335,17 @@
304335
"execution_count": 10,
305336
"metadata": {
306337
"ExecuteTime": {
307-
"end_time": "2020-05-03T17:23:26.363042Z",
308-
"start_time": "2020-05-03T17:23:26.354305Z"
338+
"end_time": "2020-05-04T17:48:22.106626Z",
339+
"start_time": "2020-05-04T17:48:22.094406Z"
309340
}
310341
},
311342
"outputs": [
312343
{
313344
"name": "stdout",
314345
"output_type": "stream",
315346
"text": [
316-
"The mean of our data is 100.03 degrees Celsius.\n",
317-
"The median of our data is 100.0 degrees Celsius.\n"
347+
"The mean of our data is 100.13 degrees Celsius.\n",
348+
"The median of our data is 99.95 degrees Celsius.\n"
318349
]
319350
}
320351
],
@@ -355,8 +386,8 @@
355386
"execution_count": 11,
356387
"metadata": {
357388
"ExecuteTime": {
358-
"end_time": "2020-05-03T17:23:26.372703Z",
359-
"start_time": "2020-05-03T17:23:26.366079Z"
389+
"end_time": "2020-05-04T17:48:22.116600Z",
390+
"start_time": "2020-05-04T17:48:22.109204Z"
360391
}
361392
},
362393
"outputs": [],
@@ -383,8 +414,8 @@
383414
"execution_count": 12,
384415
"metadata": {
385416
"ExecuteTime": {
386-
"end_time": "2020-05-03T17:23:26.394695Z",
387-
"start_time": "2020-05-03T17:23:26.376334Z"
417+
"end_time": "2020-05-04T17:48:22.134729Z",
418+
"start_time": "2020-05-04T17:48:22.119998Z"
388419
}
389420
},
390421
"outputs": [
@@ -417,8 +448,8 @@
417448
"execution_count": 13,
418449
"metadata": {
419450
"ExecuteTime": {
420-
"end_time": "2020-05-03T17:23:26.404011Z",
421-
"start_time": "2020-05-03T17:23:26.397718Z"
451+
"end_time": "2020-05-04T17:48:22.144742Z",
452+
"start_time": "2020-05-04T17:48:22.137763Z"
422453
}
423454
},
424455
"outputs": [
@@ -448,11 +479,11 @@
448479
},
449480
{
450481
"cell_type": "code",
451-
"execution_count": 16,
482+
"execution_count": 14,
452483
"metadata": {
453484
"ExecuteTime": {
454-
"end_time": "2020-05-03T17:29:16.555475Z",
455-
"start_time": "2020-05-03T17:29:16.549626Z"
485+
"end_time": "2020-05-04T17:48:22.154779Z",
486+
"start_time": "2020-05-04T17:48:22.147119Z"
456487
}
457488
},
458489
"outputs": [
@@ -462,7 +493,7 @@
462493
"array([ 0, 1, 4, -5, 3, 0])"
463494
]
464495
},
465-
"execution_count": 16,
496+
"execution_count": 14,
466497
"metadata": {},
467498
"output_type": "execute_result"
468499
}
@@ -492,7 +523,7 @@
492523
"| 4 | 78.6 $\\pm$ 0.1 |\n",
493524
"| 5 | 78.4 $\\pm$ 0.1 |\n",
494525
"| 6 | 77.9 $\\pm$ 0.1 |\n",
495-
"| 7 | 99.4 $\\pm$ 0.1 |\n",
526+
"| 7 | 79.4 $\\pm$ 0.1 |\n",
496527
"| 8 | 78.0 $\\pm$ 0.1 |\n",
497528
"| 9 | 78.7 $\\pm$ 0.1 |\n",
498529
"| 10 | 78.4 $\\pm$ 0.1 |\n",
@@ -511,8 +542,8 @@
511542
"execution_count": 15,
512543
"metadata": {
513544
"ExecuteTime": {
514-
"end_time": "2020-05-03T17:23:26.421014Z",
515-
"start_time": "2020-05-03T17:23:26.417948Z"
545+
"end_time": "2020-05-04T17:48:22.165877Z",
546+
"start_time": "2020-05-04T17:48:22.158093Z"
516547
}
517548
},
518549
"outputs": [],

0 commit comments

Comments
 (0)