Skip to content

Commit c0fd9e9

Browse files
authored
Merge pull request #1104 from finchnSNPs/patch-1
Update 02-numpy.md
2 parents bea3122 + 67b7795 commit c0fd9e9

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

episodes/02-numpy.md

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -422,28 +422,50 @@ operation across an axis:
422422

423423
![](fig/python-operations-across-axes.png){alt="Per-patient maximum inflammation is computed row-wise across all columns usingnumpy.amax(data, axis=1). Per-day average inflammation is computed column-wise across all rows usingnumpy.mean(data, axis=0)."}
424424

425-
To support this functionality,
426-
most array functions allow us to specify the axis we want to work on.
427-
If we ask for the average across axis 0 (rows in our 2D example),
428-
we get:
425+
To find the **maximum inflammation reported for each patient**, you would apply the `max` function moving across the columns (axis 1). To find the **daily average inflammation reported across patients**, you would apply the `mean` function moving down the rows (axis 0).
426+
427+
To support this functionality, most array functions allow us to specify the axis we want to work on. If we ask for the max across axis 1 (columns in our 2D example), we get:
428+
429+
```python
430+
print(numpy.max(data, axis=1))
431+
```
432+
433+
```output
434+
[18. 18. 19. 17. 17. 18. 17. 20. 17. 18. 18. 18. 17. 16. 17. 18. 19. 19.
435+
17. 19. 19. 16. 17. 15. 17. 17. 18. 17. 20. 17. 16. 19. 15. 15. 19. 17.
436+
16. 17. 19. 16. 18. 19. 16. 19. 18. 16. 19. 15. 16. 18. 14. 20. 17. 15.
437+
17. 16. 17. 19. 18. 18.]
438+
```
439+
440+
As a quick check, we can ask this array what its shape is. We expect 60 patient maximums:
441+
442+
```python
443+
print(numpy.max(data, axis=1).shape)
444+
```
445+
446+
```output
447+
(60,)
448+
```
449+
450+
The expression `(60,)` tells us we have an N×1 vector, so this is the maximum inflammation per day for each patients.
451+
452+
If we ask for the average across/down axis 0 (rows in our 2D example), we get:
429453

430454
```python
431455
print(numpy.mean(data, axis=0))
432456
```
433457

434458
```output
435-
[ 0. 0.45 1.11666667 1.75 2.43333333 3.15
436-
3.8 3.88333333 5.23333333 5.51666667 5.95 5.9
437-
8.35 7.73333333 8.36666667 9.5 9.58333333
438-
10.63333333 11.56666667 12.35 13.25 11.96666667
439-
11.03333333 10.16666667 10. 8.66666667 9.15 7.25
440-
7.33333333 6.58333333 6.06666667 5.95 5.11666667 3.6
441-
3.3 3.56666667 2.48333333 1.5 1.13333333
442-
0.56666667]
459+
[ 0. 0.45 1.11666667 1.75 2.43333333 3.15
460+
3.8 3.88333333 5.23333333 5.51666667 5.95 5.9
461+
8.35 7.73333333 8.36666667 9.5 9.58333333 10.63333333
462+
11.56666667 12.35 13.25 11.96666667 11.03333333 10.16666667
463+
10. 8.66666667 9.15 7.25 7.33333333 6.58333333
464+
6.06666667 5.95 5.11666667 3.6 3.3 3.56666667
465+
2.48333333 1.5 1.13333333 0.56666667]
443466
```
444467

445-
As a quick check,
446-
we can ask this array what its shape is:
468+
Check the array shape. We expect 40 averages, one for each day of the study:
447469

448470
```python
449471
print(numpy.mean(data, axis=0).shape)
@@ -452,26 +474,19 @@ print(numpy.mean(data, axis=0).shape)
452474
```output
453475
(40,)
454476
```
455-
456-
The expression `(40,)` tells us we have an N×1 vector,
457-
so this is the average inflammation per day for all patients.
458-
If we average across axis 1 (columns in our 2D example), we get:
477+
Similarly, we can apply the `mean` function to axis 1 to get the patient's average inflammation over the duration of the study (60 values).
459478

460479
```python
461480
print(numpy.mean(data, axis=1))
462481
```
463-
464482
```output
465-
[ 5.45 5.425 6.1 5.9 5.55 6.225 5.975 6.65 6.625 6.525
466-
6.775 5.8 6.225 5.75 5.225 6.3 6.55 5.7 5.85 6.55
467-
5.775 5.825 6.175 6.1 5.8 6.425 6.05 6.025 6.175 6.55
468-
6.175 6.35 6.725 6.125 7.075 5.725 5.925 6.15 6.075 5.75
469-
5.975 5.725 6.3 5.9 6.75 5.925 7.225 6.15 5.95 6.275 5.7
470-
6.1 6.825 5.975 6.725 5.7 6.25 6.4 7.05 5.9 ]
483+
[5.45 5.425 6.1 5.9 5.55 6.225 5.975 6.65 6.625 6.525 6.775 5.8
484+
6.225 5.75 5.225 6.3 6.55 5.7 5.85 6.55 5.775 5.825 6.175 6.1
485+
5.8 6.425 6.05 6.025 6.175 6.55 6.175 6.35 6.725 6.125 7.075 5.725
486+
5.925 6.15 6.075 5.75 5.975 5.725 6.3 5.9 6.75 5.925 7.225 6.15
487+
5.95 6.275 5.7 6.1 6.825 5.975 6.725 5.7 6.25 6.4 7.05 5.9 ]
471488
```
472489

473-
which is the average inflammation per patient across all days.
474-
475490
::::::::::::::::::::::::::::::::::::::: challenge
476491

477492
## Slicing Strings

0 commit comments

Comments
 (0)