Skip to content

Commit fc5ed62

Browse files
committed
fix: use amin/amax from numpy
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 4ccb50d commit fc5ed62

File tree

15 files changed

+77
-77
lines changed

15 files changed

+77
-77
lines changed

_episodes/02-numpy.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ keypoints:
1818
- "Array indices start at 0, not 1."
1919
- "Use `low:high` to specify a `slice` that includes the indices from `low` to `high-1`."
2020
- "Use `# some kind of explanation` to add comments to programs."
21-
- "Use `numpy.mean(array)`, `numpy.max(array)`, and `numpy.min(array)` to calculate simple statistics."
21+
- "Use `numpy.mean(array)`, `numpy.amax(array)`, and `numpy.amin(array)` to calculate simple statistics."
2222
- "Use `numpy.mean(array, axis=0)` or `numpy.mean(array, axis=1)` to calculate statistics across the specified axis."
2323
---
2424

@@ -359,16 +359,16 @@ We'll also use multiple assignment,
359359
a convenient Python feature that will enable us to do this all in one line.
360360
361361
~~~
362-
maxval, minval, stdval = numpy.max(data), numpy.min(data), numpy.std(data)
362+
maxval, minval, stdval = numpy.amax(data), numpy.amin(data), numpy.std(data)
363363

364364
print('maximum inflammation:', maxval)
365365
print('minimum inflammation:', minval)
366366
print('standard deviation:', stdval)
367367
~~~
368368
{: .language-python}
369369
370-
Here we've assigned the return value from `numpy.max(data)` to the variable `maxval`, the value
371-
from `numpy.min(data)` to `minval`, and so on.
370+
Here we've assigned the return value from `numpy.amax(data)` to the variable `maxval`, the value
371+
from `numpy.amin(data)` to `minval`, and so on.
372372
373373
~~~
374374
maximum inflammation: 20.0
@@ -402,7 +402,7 @@ then ask it to do the calculation:
402402
403403
~~~
404404
patient_0 = data[0, :] # 0 on the first axis (rows), everything on the second (columns)
405-
print('maximum inflammation for patient 0:', numpy.max(patient_0))
405+
print('maximum inflammation for patient 0:', numpy.amax(patient_0))
406406
~~~
407407
{: .language-python}
408408
@@ -420,7 +420,7 @@ We don't actually need to store the row in a variable of its own.
420420
Instead, we can combine the selection and the function call:
421421
422422
~~~
423-
print('maximum inflammation for patient 2:', numpy.max(data[2, :]))
423+
print('maximum inflammation for patient 2:', numpy.amax(data[2, :]))
424424
~~~
425425
{: .language-python}
426426
@@ -435,7 +435,7 @@ diagram on the right)? As the diagram below shows, we want to perform the
435435
operation across an axis:
436436
437437
![Per-patient maximum inflammation is computed row-wise across all columns using
438-
numpy.max(data, axis=1). Per-day average inflammation is computed column-wise across all rows using
438+
numpy.amax(data, axis=1). Per-day average inflammation is computed column-wise across all rows using
439439
numpy.mean(data, axis=0).](../fig/python-operations-across-axes.png)
440440
441441
To support this functionality,
@@ -754,11 +754,11 @@ which is the average inflammation per patient across all days.
754754
> it matter if the change in inflammation is an increase or a decrease?
755755
>
756756
> > ## Solution
757-
> > By using the `numpy.max()` function after you apply the `numpy.diff()`
757+
> > By using the `numpy.amax()` function after you apply the `numpy.diff()`
758758
> > function, you will get the largest difference between days.
759759
> >
760760
> > ~~~
761-
> > numpy.max(numpy.diff(data, axis=1), axis=1)
761+
> > numpy.amax(numpy.diff(data, axis=1), axis=1)
762762
> > ~~~
763763
> > {: .language-python}
764764
> >
@@ -781,7 +781,7 @@ which is the average inflammation per patient across all days.
781781
> > between readings.
782782
> >
783783
> > ~~~
784-
> > numpy.max(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
784+
> > numpy.amax(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
785785
> > ~~~
786786
> > {: .language-python}
787787
> >

_episodes/03-matplotlib.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ the medication takes 3 weeks to take effect. But a good data scientist doesn't
7474
average of a dataset, so let's have a look at two other statistics:
7575
7676
~~~
77-
max_plot = matplotlib.pyplot.plot(numpy.max(data, axis=0))
77+
max_plot = matplotlib.pyplot.plot(numpy.amax(data, axis=0))
7878
matplotlib.pyplot.show()
7979
~~~
8080
{: .language-python}
8181
8282
![A line graph showing the maximum inflammation across all patients over a 40-day period.](../fig/inflammation-01-maximum.svg)
8383
8484
~~~
85-
min_plot = matplotlib.pyplot.plot(numpy.min(data, axis=0))
85+
min_plot = matplotlib.pyplot.plot(numpy.amin(data, axis=0))
8686
matplotlib.pyplot.show()
8787
~~~
8888
{: .language-python}
@@ -123,10 +123,10 @@ axes1.set_ylabel('average')
123123
axes1.plot(numpy.mean(data, axis=0))
124124

125125
axes2.set_ylabel('max')
126-
axes2.plot(numpy.max(data, axis=0))
126+
axes2.plot(numpy.amax(data, axis=0))
127127

128128
axes3.set_ylabel('min')
129-
axes3.plot(numpy.min(data, axis=0))
129+
axes3.plot(numpy.amin(data, axis=0))
130130

131131
fig.tight_layout()
132132

@@ -199,7 +199,7 @@ formats, including SVG, PDF, and JPEG.
199199
> > ~~~
200200
> > # One method
201201
> > axes3.set_ylabel('min')
202-
> > axes3.plot(numpy.min(data, axis=0))
202+
> > axes3.plot(numpy.amin(data, axis=0))
203203
> > axes3.set_ylim(0,6)
204204
> > ~~~
205205
> > {: .language-python}
@@ -208,10 +208,10 @@ formats, including SVG, PDF, and JPEG.
208208
> > ## Solution
209209
> > ~~~
210210
> > # A more automated approach
211-
> > min_data = numpy.min(data, axis=0)
211+
> > min_data = numpy.amin(data, axis=0)
212212
> > axes3.set_ylabel('min')
213213
> > axes3.plot(min_data)
214-
> > axes3.set_ylim(numpy.min(min_data), numpy.max(min_data) * 1.1)
214+
> > axes3.set_ylim(numpy.amin(min_data), numpy.amax(min_data) * 1.1)
215215
> > ~~~
216216
> > {: .language-python}
217217
> {: .solution}
@@ -244,10 +244,10 @@ formats, including SVG, PDF, and JPEG.
244244
> > axes1.plot(numpy.mean(data, axis=0), drawstyle='steps-mid')
245245
> >
246246
> > axes2.set_ylabel('max')
247-
> > axes2.plot(numpy.max(data, axis=0), drawstyle='steps-mid')
247+
> > axes2.plot(numpy.amax(data, axis=0), drawstyle='steps-mid')
248248
> >
249249
> > axes3.set_ylabel('min')
250-
> > axes3.plot(numpy.min(data, axis=0), drawstyle='steps-mid')
250+
> > axes3.plot(numpy.amin(data, axis=0), drawstyle='steps-mid')
251251
> >
252252
> > fig.tight_layout()
253253
> >
@@ -297,10 +297,10 @@ formats, including SVG, PDF, and JPEG.
297297
> > axes1.plot(numpy.mean(data, axis=0))
298298
> >
299299
> > axes2.set_ylabel('max')
300-
> > axes2.plot(numpy.max(data, axis=0))
300+
> > axes2.plot(numpy.amax(data, axis=0))
301301
> >
302302
> > axes3.set_ylabel('min')
303-
> > axes3.plot(numpy.min(data, axis=0))
303+
> > axes3.plot(numpy.amin(data, axis=0))
304304
> >
305305
> > fig.tight_layout()
306306
> >

_episodes/06-files.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ for filename in filenames:
7171
axes1.plot(numpy.mean(data, axis=0))
7272
7373
axes2.set_ylabel('max')
74-
axes2.plot(numpy.max(data, axis=0))
74+
axes2.plot(numpy.amax(data, axis=0))
7575
7676
axes3.set_ylabel('min')
77-
axes3.plot(numpy.min(data, axis=0))
77+
axes3.plot(numpy.amin(data, axis=0))
7878
7979
fig.tight_layout()
8080
matplotlib.pyplot.show()
@@ -200,10 +200,10 @@ flare-ups at all throughout the trial, suggesting that they may not even suffer
200200
> > axes1.plot(numpy.mean(composite_data, axis=0))
201201
> >
202202
> > axes2.set_ylabel('max')
203-
> > axes2.plot(numpy.max(composite_data, axis=0))
203+
> > axes2.plot(numpy.amax(composite_data, axis=0))
204204
> >
205205
> > axes3.set_ylabel('min')
206-
> > axes3.plot(numpy.min(composite_data, axis=0))
206+
> > axes3.plot(numpy.amin(composite_data, axis=0))
207207
> >
208208
> > fig.tight_layout()
209209
> >

_episodes/07-cond.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ if maximum inflammation in the beginning (day 0) and in the middle (day 20) of
168168
the study are equal to the corresponding day numbers.
169169

170170
~~~
171-
max_inflammation_0 = numpy.max(data, axis=0)[0]
172-
max_inflammation_20 = numpy.max(data, axis=0)[20]
171+
max_inflammation_0 = numpy.amax(data, axis=0)[0]
172+
max_inflammation_20 = numpy.amax(data, axis=0)[20]
173173
174174
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
175175
print('Suspicious looking maxima!')
@@ -181,7 +181,7 @@ the minima per day were all zero (looks like a healthy person snuck into our stu
181181
We can also check for this with an `elif` condition:
182182

183183
~~~
184-
elif numpy.sum(numpy.min(data, axis=0)) == 0:
184+
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
185185
print('Minima add up to zero!')
186186
~~~
187187
{: .language-python}
@@ -199,12 +199,12 @@ Let's test that out:
199199
~~~
200200
data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
201201
202-
max_inflammation_0 = numpy.max(data, axis=0)[0]
203-
max_inflammation_20 = numpy.max(data, axis=0)[20]
202+
max_inflammation_0 = numpy.amax(data, axis=0)[0]
203+
max_inflammation_20 = numpy.amax(data, axis=0)[20]
204204
205205
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
206206
print('Suspicious looking maxima!')
207-
elif numpy.sum(numpy.min(data, axis=0)) == 0:
207+
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
208208
print('Minima add up to zero!')
209209
else:
210210
print('Seems OK!')
@@ -219,12 +219,12 @@ Suspicious looking maxima!
219219
~~~
220220
data = numpy.loadtxt(fname='inflammation-03.csv', delimiter=',')
221221
222-
max_inflammation_0 = numpy.max(data, axis=0)[0]
223-
max_inflammation_20 = numpy.max(data, axis=0)[20]
222+
max_inflammation_0 = numpy.amax(data, axis=0)[0]
223+
max_inflammation_20 = numpy.amax(data, axis=0)[20]
224224
225225
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
226226
print('Suspicious looking maxima!')
227-
elif numpy.sum(numpy.min(data, axis=0)) == 0:
227+
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
228228
print('Minima add up to zero!')
229229
else:
230230
print('Seems OK!')

_episodes/08-func.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ def visualize(filename):
222222
axes1.plot(numpy.mean(data, axis=0))
223223
224224
axes2.set_ylabel('max')
225-
axes2.plot(numpy.max(data, axis=0))
225+
axes2.plot(numpy.amax(data, axis=0))
226226
227227
axes3.set_ylabel('min')
228-
axes3.plot(numpy.min(data, axis=0))
228+
axes3.plot(numpy.amin(data, axis=0))
229229
230230
fig.tight_layout()
231231
matplotlib.pyplot.show()
@@ -240,9 +240,9 @@ def detect_problems(filename):
240240
241241
data = numpy.loadtxt(fname=filename, delimiter=',')
242242
243-
if numpy.max(data, axis=0)[0] == 0 and numpy.max(data, axis=0)[20] == 20:
243+
if numpy.amax(data, axis=0)[0] == 0 and numpy.amax(data, axis=0)[20] == 20:
244244
print('Suspicious looking maxima!')
245-
elif numpy.sum(numpy.min(data, axis=0)) == 0:
245+
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
246246
print('Minima add up to zero!')
247247
else:
248248
print('Seems OK!')
@@ -330,12 +330,12 @@ It's hard to tell from the default output whether the result is correct,
330330
but there are a few tests that we can run to reassure us:
331331

332332
~~~
333-
print('original min, mean, and max are:', numpy.min(data), numpy.mean(data), numpy.max(data))
333+
print('original min, mean, and max are:', numpy.amin(data), numpy.mean(data), numpy.amax(data))
334334
offset_data = offset_mean(data, 0)
335335
print('min, mean, and max of offset data are:',
336-
numpy.min(offset_data),
336+
numpy.amin(offset_data),
337337
numpy.mean(offset_data),
338-
numpy.max(offset_data))
338+
numpy.amax(offset_data))
339339
~~~
340340
{: .language-python}
341341

@@ -806,8 +806,8 @@ readable code!
806806
> > ## Solution
807807
> > ~~~
808808
> > def rescale(input_array):
809-
> > L = numpy.min(input_array)
810-
> > H = numpy.max(input_array)
809+
> > L = numpy.amin(input_array)
810+
> > H = numpy.amax(input_array)
811811
> > output_array = (input_array - L) / (H - L)
812812
> > return output_array
813813
> > ~~~
@@ -851,8 +851,8 @@ readable code!
851851
> > ~~~
852852
> > def rescale(input_array, low_val=0.0, high_val=1.0):
853853
> > """rescales input array values to lie between low_val and high_val"""
854-
> > L = numpy.min(input_array)
855-
> > H = numpy.max(input_array)
854+
> > L = numpy.amin(input_array)
855+
> > H = numpy.amax(input_array)
856856
> > intermed_array = (input_array - L) / (H - L)
857857
> > output_array = intermed_array * (high_val - low_val) + low_val
858858
> > return output_array

_episodes/10-defensive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ This violates another important rule of programming:
482482
> > # a possible pre-condition:
483483
> > assert len(input_list) > 0, 'List length must be non-zero'
484484
> > # a possible post-condition:
485-
> > assert numpy.min(input_list) <= average <= numpy.max(input_list),
485+
> > assert numpy.amin(input_list) <= average <= numpy.amax(input_list),
486486
> > 'Average should be between min and max of input values (inclusive)'
487487
> > ~~~
488488
> > {: .language-python}

_episodes/12-cmdline.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,11 @@ def main():
460460
data = numpy.loadtxt(filename, delimiter=',')
461461

462462
if action == '--min':
463-
values = numpy.min(data, axis=1)
463+
values = numpy.amin(data, axis=1)
464464
elif action == '--mean':
465465
values = numpy.mean(data, axis=1)
466466
elif action == '--max':
467-
values = numpy.max(data, axis=1)
467+
values = numpy.amax(data, axis=1)
468468

469469
for val in values:
470470
print(val)
@@ -527,11 +527,11 @@ def process(filename, action):
527527
data = numpy.loadtxt(filename, delimiter=',')
528528

529529
if action == '--min':
530-
values = numpy.min(data, axis=1)
530+
values = numpy.amin(data, axis=1)
531531
elif action == '--mean':
532532
values = numpy.mean(data, axis=1)
533533
elif action == '--max':
534-
values = numpy.max(data, axis=1)
534+
values = numpy.amax(data, axis=1)
535535

536536
for val in values:
537537
print(val)
@@ -632,11 +632,11 @@ def process(filename, action):
632632
data = numpy.loadtxt(filename, delimiter=',')
633633

634634
if action == '--min':
635-
values = numpy.min(data, axis=1)
635+
values = numpy.amin(data, axis=1)
636636
elif action == '--mean':
637637
values = numpy.mean(data, axis=1)
638638
elif action == '--max':
639-
values = numpy.max(data, axis=1)
639+
values = numpy.amax(data, axis=1)
640640

641641
for val in values:
642642
print(val)
@@ -793,11 +793,11 @@ the program now does everything we set out to do.
793793
> > data = numpy.loadtxt(filename, delimiter=',')
794794
> >
795795
> > if action == '-n':
796-
> > values = numpy.min(data, axis=1)
796+
> > values = numpy.amin(data, axis=1)
797797
> > elif action == '-m':
798798
> > values = numpy.mean(data, axis=1)
799799
> > elif action == '-x':
800-
> > values = numpy.max(data, axis=1)
800+
> > values = numpy.amax(data, axis=1)
801801
> >
802802
> > for val in values:
803803
> > print(val)
@@ -844,11 +844,11 @@ the program now does everything we set out to do.
844844
> > data = numpy.loadtxt(filename, delimiter=',')
845845
> >
846846
> > if action == '--min':
847-
> > values = numpy.min(data, axis=1)
847+
> > values = numpy.amin(data, axis=1)
848848
> > elif action == '--mean':
849849
> > values = numpy.mean(data, axis=1)
850850
> > elif action == '--max':
851-
> > values = numpy.max(data, axis=1)
851+
> > values = numpy.amax(data, axis=1)
852852
> >
853853
> > for val in values:
854854
> > print(val)
@@ -890,11 +890,11 @@ the program now does everything we set out to do.
890890
> > data = numpy.loadtxt(filename, delimiter=',')
891891
> >
892892
> > if action == '--min':
893-
> > values = numpy.min(data, axis=1)
893+
> > values = numpy.amin(data, axis=1)
894894
> > elif action == '--mean':
895895
> > values = numpy.mean(data, axis=1)
896896
> > elif action == '--max':
897-
> > values = numpy.max(data, axis=1)
897+
> > values = numpy.amax(data, axis=1)
898898
> >
899899
> > for val in values:
900900
> > print(val)

0 commit comments

Comments
 (0)