Skip to content

Commit 27315f7

Browse files
authored
Merge pull request #995 from henryiii/henryiii/fix/anumpy
fix: use amin/amax from numpy Hi @henryiii , Thank you for the pull request. Since numpy.min and numpy.max don't actually exist on the official documentation so it would also make sense to change them to numpy.amin and numpy.amax in the lesson.
2 parents 2157d89 + 4b95a20 commit 27315f7

File tree

15 files changed

+78
-77
lines changed

15 files changed

+78
-77
lines changed

_episodes/02-numpy.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ keypoints:
1717
- "Use `array[x, y]` to select a single element from a 2D array."
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`."
20-
- "Use `numpy.mean(array)`, `numpy.max(array)`, and `numpy.min(array)` to calculate simple statistics."
20+
- "Use `# some kind of explanation` to add comments to programs."
21+
- "Use `numpy.mean(array)`, `numpy.amax(array)`, and `numpy.amin(array)` to calculate simple statistics."
2122
- "Use `numpy.mean(array, axis=0)` or `numpy.mean(array, axis=1)` to calculate statistics across the specified axis."
2223
---
2324

@@ -357,16 +358,16 @@ We'll also use multiple assignment,
357358
a convenient Python feature that will enable us to do this all in one line.
358359
359360
~~~
360-
maxval, minval, stdval = numpy.max(data), numpy.min(data), numpy.std(data)
361+
maxval, minval, stdval = numpy.amax(data), numpy.amin(data), numpy.std(data)
361362

362363
print('maximum inflammation:', maxval)
363364
print('minimum inflammation:', minval)
364365
print('standard deviation:', stdval)
365366
~~~
366367
{: .language-python}
367368
368-
Here we've assigned the return value from `numpy.max(data)` to the variable `maxval`, the value
369-
from `numpy.min(data)` to `minval`, and so on.
369+
Here we've assigned the return value from `numpy.amax(data)` to the variable `maxval`, the value
370+
from `numpy.amin(data)` to `minval`, and so on.
370371
371372
~~~
372373
maximum inflammation: 20.0
@@ -400,7 +401,7 @@ then ask it to do the calculation:
400401
401402
~~~
402403
patient_0 = data[0, :] # 0 on the first axis (rows), everything on the second (columns)
403-
print('maximum inflammation for patient 0:', numpy.max(patient_0))
404+
print('maximum inflammation for patient 0:', numpy.amax(patient_0))
404405
~~~
405406
{: .language-python}
406407
@@ -413,7 +414,7 @@ We don't actually need to store the row in a variable of its own.
413414
Instead, we can combine the selection and the function call:
414415
415416
~~~
416-
print('maximum inflammation for patient 2:', numpy.max(data[2, :]))
417+
print('maximum inflammation for patient 2:', numpy.amax(data[2, :]))
417418
~~~
418419
{: .language-python}
419420
@@ -428,7 +429,7 @@ diagram on the right)? As the diagram below shows, we want to perform the
428429
operation across an axis:
429430
430431
![Per-patient maximum inflammation is computed row-wise across all columns using
431-
numpy.max(data, axis=1). Per-day average inflammation is computed column-wise across all rows using
432+
numpy.amax(data, axis=1). Per-day average inflammation is computed column-wise across all rows using
432433
numpy.mean(data, axis=0).](../fig/python-operations-across-axes.png)
433434
434435
To support this functionality,
@@ -747,11 +748,11 @@ which is the average inflammation per patient across all days.
747748
> it matter if the change in inflammation is an increase or a decrease?
748749
>
749750
> > ## Solution
750-
> > By using the `numpy.max()` function after you apply the `numpy.diff()`
751+
> > By using the `numpy.amax()` function after you apply the `numpy.diff()`
751752
> > function, you will get the largest difference between days.
752753
> >
753754
> > ~~~
754-
> > numpy.max(numpy.diff(data, axis=1), axis=1)
755+
> > numpy.amax(numpy.diff(data, axis=1), axis=1)
755756
> > ~~~
756757
> > {: .language-python}
757758
> >
@@ -774,7 +775,7 @@ which is the average inflammation per patient across all days.
774775
> > between readings.
775776
> >
776777
> > ~~~
777-
> > numpy.max(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
778+
> > numpy.amax(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
778779
> > ~~~
779780
> > {: .language-python}
780781
> >

_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
@@ -241,10 +241,10 @@ def visualize(filename):
241241
axes1.plot(numpy.mean(data, axis=0))
242242
243243
axes2.set_ylabel('max')
244-
axes2.plot(numpy.max(data, axis=0))
244+
axes2.plot(numpy.amax(data, axis=0))
245245
246246
axes3.set_ylabel('min')
247-
axes3.plot(numpy.min(data, axis=0))
247+
axes3.plot(numpy.amin(data, axis=0))
248248
249249
fig.tight_layout()
250250
matplotlib.pyplot.show()
@@ -259,9 +259,9 @@ def detect_problems(filename):
259259
260260
data = numpy.loadtxt(fname=filename, delimiter=',')
261261
262-
if numpy.max(data, axis=0)[0] == 0 and numpy.max(data, axis=0)[20] == 20:
262+
if numpy.amax(data, axis=0)[0] == 0 and numpy.amax(data, axis=0)[20] == 20:
263263
print('Suspicious looking maxima!')
264-
elif numpy.sum(numpy.min(data, axis=0)) == 0:
264+
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
265265
print('Minima add up to zero!')
266266
else:
267267
print('Seems OK!')
@@ -349,12 +349,12 @@ It's hard to tell from the default output whether the result is correct,
349349
but there are a few tests that we can run to reassure us:
350350

351351
~~~
352-
print('original min, mean, and max are:', numpy.min(data), numpy.mean(data), numpy.max(data))
352+
print('original min, mean, and max are:', numpy.amin(data), numpy.mean(data), numpy.amax(data))
353353
offset_data = offset_mean(data, 0)
354354
print('min, mean, and max of offset data are:',
355-
numpy.min(offset_data),
355+
numpy.amin(offset_data),
356356
numpy.mean(offset_data),
357-
numpy.max(offset_data))
357+
numpy.amax(offset_data))
358358
~~~
359359
{: .language-python}
360360

@@ -825,8 +825,8 @@ readable code!
825825
> > ## Solution
826826
> > ~~~
827827
> > def rescale(input_array):
828-
> > L = numpy.min(input_array)
829-
> > H = numpy.max(input_array)
828+
> > L = numpy.amin(input_array)
829+
> > H = numpy.amax(input_array)
830830
> > output_array = (input_array - L) / (H - L)
831831
> > return output_array
832832
> > ~~~
@@ -870,8 +870,8 @@ readable code!
870870
> > ~~~
871871
> > def rescale(input_array, low_val=0.0, high_val=1.0):
872872
> > """rescales input array values to lie between low_val and high_val"""
873-
> > L = numpy.min(input_array)
874-
> > H = numpy.max(input_array)
873+
> > L = numpy.amin(input_array)
874+
> > H = numpy.amax(input_array)
875875
> > intermed_array = (input_array - L) / (H - L)
876876
> > output_array = intermed_array * (high_val - low_val) + low_val
877877
> > return output_array

_episodes/10-defensive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ This violates another important rule of programming:
489489
> > # a possible pre-condition:
490490
> > assert len(input_list) > 0, 'List length must be non-zero'
491491
> > # a possible post-condition:
492-
> > assert numpy.min(input_list) <= average <= numpy.max(input_list),
492+
> > assert numpy.amin(input_list) <= average <= numpy.amax(input_list),
493493
> > 'Average should be between min and max of input values (inclusive)'
494494
> > ~~~
495495
> > {: .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)