Skip to content

Commit eb18f24

Browse files
committed
replace all instances of numpy.amax and numpy.amin with numpy.max and numpy.min per discussion on #1117
1 parent ddcf70f commit eb18f24

18 files changed

Lines changed: 474 additions & 92 deletions

episodes/02-numpy.md

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,15 @@ We'll also use multiple assignment,
340340
a convenient Python feature that will enable us to do this all in one line.
341341

342342
```python
343-
maxval, minval, stdval = numpy.amax(data), numpy.amin(data), numpy.std(data)
343+
maxval, minval, stdval = numpy.max(data), numpy.min(data), numpy.std(data)
344344

345345
print('maximum inflammation:', maxval)
346346
print('minimum inflammation:', minval)
347347
print('standard deviation:', stdval)
348348
```
349349

350-
Here we've assigned the return value from `numpy.amax(data)` to the variable `maxval`, the value
351-
from `numpy.amin(data)` to `minval`, and so on.
350+
Here we've assigned the return value from `numpy.max(data)` to the variable `maxval`, the value
351+
from `numpy.min(data)` to `minval`, and so on.
352352

353353
```output
354354
maximum inflammation: 20.0
@@ -374,18 +374,6 @@ and press the <kbd>Tab</kbd> key twice for a listing of what is available. You c
374374
for example: `help(numpy.cumprod)`.
375375

376376

377-
::::::::::::::::::::::::::::::::::::::::::::::::::
378-
379-
::::::::::::::::::::::::::::::::::::::::: callout
380-
381-
## Confusing Function Names
382-
383-
One might wonder why the functions are called `amax` and `amin` and not `max` and `min` or why the other is called `mean` and not `amean`.
384-
The package `numpy` does provide functions `max` and `min` that are fully equivalent to `amax` and `amin`, but they share a name with standard library functions `max` and `min` that come with Python itself.
385-
Referring to the functions like we did above, that is `numpy.max` for example, does not cause problems, but there are other ways to refer to them that could.
386-
In addition, text editors might highlight (color) these functions like standard library function, even though they belong to NumPy, which can be confusing and lead to errors.
387-
Since there is no function called `mean` in the standard library, there is no function called `amean`.
388-
389377
::::::::::::::::::::::::::::::::::::::::::::::::::
390378

391379
When analyzing data, though,
@@ -397,7 +385,7 @@ then ask it to do the calculation:
397385

398386
```python
399387
patient_0 = data[0, :] # 0 on the first axis (rows), everything on the second (columns)
400-
print('maximum inflammation for patient 0:', numpy.amax(patient_0))
388+
print('maximum inflammation for patient 0:', numpy.max(patient_0))
401389
```
402390

403391
```output
@@ -408,7 +396,7 @@ We don't actually need to store the row in a variable of its own.
408396
Instead, we can combine the selection and the function call:
409397

410398
```python
411-
print('maximum inflammation for patient 2:', numpy.amax(data[2, :]))
399+
print('maximum inflammation for patient 2:', numpy.max(data[2, :]))
412400
```
413401

414402
```output
@@ -420,11 +408,11 @@ next diagram on the left) or the average for each day (as in the
420408
diagram on the right)? As the diagram below shows, we want to perform the
421409
operation across an axis:
422410

423-
![](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)."}
411+
![](fig/python-operations-across-axes.svg){alt="Per-patient maximum inflammation is computed row-wise across all columns usingnumpy.max(data, axis=1). Per-day average inflammation is computed column-wise across all rows usingnumpy.mean(data, axis=0)."}
424412

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).
413+
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).
426414

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:
415+
To support this functionality, most array functions allow us to specify the axis we want to work on. If we ask for the maximum across axis 1 (columns in our 2D example), we get:
428416

429417
```python
430418
print(numpy.max(data, axis=1))
@@ -437,7 +425,7 @@ print(numpy.max(data, axis=1))
437425
17. 16. 17. 19. 18. 18.]
438426
```
439427

440-
As a quick check, we can ask this array what its shape is. We expect 60 patient maximums:
428+
As a quick check, we can ask this array what its shape is. We expect 60 patient maxima:
441429

442430
```python
443431
print(numpy.max(data, axis=1).shape)
@@ -779,11 +767,11 @@ it matter if the change in inflammation is an increase or a decrease?
779767

780768
## Solution
781769

782-
By using the `numpy.amax()` function after you apply the `numpy.diff()`
770+
By using the `numpy.max()` function after you apply the `numpy.diff()`
783771
function, you will get the largest difference between days.
784772

785773
```python
786-
numpy.amax(numpy.diff(data, axis=1), axis=1)
774+
numpy.max(numpy.diff(data, axis=1), axis=1)
787775
```
788776

789777
```python
@@ -804,7 +792,7 @@ Notice the difference if you get the largest *absolute* difference
804792
between readings.
805793

806794
```python
807-
numpy.amax(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
795+
numpy.max(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
808796
```
809797

810798
```python
@@ -831,7 +819,7 @@ array([ 12., 14., 11., 13., 11., 13., 10., 12., 10., 10., 10.,
831819
- Array indices start at 0, not 1.
832820
- Use `low:high` to specify a `slice` that includes the indices from `low` to `high-1`.
833821
- Use `# some kind of explanation` to add comments to programs.
834-
- Use `numpy.mean(array)`, `numpy.amax(array)`, and `numpy.amin(array)` to calculate simple statistics.
822+
- Use `numpy.mean(array)`, `numpy.max(array)`, and `numpy.min(array)` to calculate simple statistics.
835823
- Use `numpy.mean(array, axis=0)` or `numpy.mean(array, axis=1)` to calculate statistics across the specified axis.
836824

837825
::::::::::::::::::::::::::::::::::::::::::::::::::

episodes/03-matplotlib.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ the medication takes 3 weeks to take effect. But a good data scientist doesn't
7979
average of a dataset, so let's have a look at two other statistics:
8080

8181
```python
82-
max_plot = matplotlib.pyplot.plot(numpy.amax(data, axis=0))
82+
max_plot = matplotlib.pyplot.plot(numpy.max(data, axis=0))
8383
matplotlib.pyplot.show()
8484
```
8585

8686
![](fig/inflammation-01-maximum.svg){alt='A line graph showing the maximum inflammation across all patients over a 40-day period.'}
8787

8888
```python
89-
min_plot = matplotlib.pyplot.plot(numpy.amin(data, axis=0))
89+
min_plot = matplotlib.pyplot.plot(numpy.min(data, axis=0))
9090
matplotlib.pyplot.show()
9191
```
9292

@@ -127,10 +127,10 @@ axes1.set_ylabel('average')
127127
axes1.plot(numpy.mean(data, axis=0))
128128

129129
axes2.set_ylabel('max')
130-
axes2.plot(numpy.amax(data, axis=0))
130+
axes2.plot(numpy.max(data, axis=0))
131131

132132
axes3.set_ylabel('min')
133-
axes3.plot(numpy.amin(data, axis=0))
133+
axes3.plot(numpy.min(data, axis=0))
134134

135135
fig.tight_layout()
136136

@@ -215,7 +215,7 @@ Update your plotting code to automatically set a more appropriate scale.
215215
```python
216216
# One method
217217
axes3.set_ylabel('min')
218-
axes3.plot(numpy.amin(data, axis=0))
218+
axes3.plot(numpy.min(data, axis=0))
219219
axes3.set_ylim(0, 6)
220220
```
221221

@@ -227,10 +227,10 @@ axes3.set_ylim(0, 6)
227227

228228
```python
229229
# A more automated approach
230-
min_data = numpy.amin(data, axis=0)
230+
min_data = numpy.min(data, axis=0)
231231
axes3.set_ylabel('min')
232232
axes3.plot(min_data)
233-
axes3.set_ylim(numpy.amin(min_data), numpy.amax(min_data) * 1.1)
233+
axes3.set_ylim(numpy.min(min_data), numpy.max(min_data) * 1.1)
234234
```
235235

236236
:::::::::::::::::::::::::
@@ -269,10 +269,10 @@ axes1.set_ylabel('average')
269269
axes1.plot(numpy.mean(data, axis=0), drawstyle='steps-mid')
270270

271271
axes2.set_ylabel('max')
272-
axes2.plot(numpy.amax(data, axis=0), drawstyle='steps-mid')
272+
axes2.plot(numpy.max(data, axis=0), drawstyle='steps-mid')
273273

274274
axes3.set_ylabel('min')
275-
axes3.plot(numpy.amin(data, axis=0), drawstyle='steps-mid')
275+
axes3.plot(numpy.min(data, axis=0), drawstyle='steps-mid')
276276

277277
fig.tight_layout()
278278

@@ -336,10 +336,10 @@ axes1.set_ylabel('average')
336336
axes1.plot(numpy.mean(data, axis=0))
337337

338338
axes2.set_ylabel('max')
339-
axes2.plot(numpy.amax(data, axis=0))
339+
axes2.plot(numpy.max(data, axis=0))
340340

341341
axes3.set_ylabel('min')
342-
axes3.plot(numpy.amin(data, axis=0))
342+
axes3.plot(numpy.min(data, axis=0))
343343

344344
fig.tight_layout()
345345

episodes/06-files.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ for filename in filenames:
7474
axes1.plot(numpy.mean(data, axis=0))
7575

7676
axes2.set_ylabel('max')
77-
axes2.plot(numpy.amax(data, axis=0))
77+
axes2.plot(numpy.max(data, axis=0))
7878

7979
axes3.set_ylabel('min')
80-
axes3.plot(numpy.amin(data, axis=0))
80+
axes3.plot(numpy.min(data, axis=0))
8181

8282
fig.tight_layout()
8383
matplotlib.pyplot.show()
@@ -199,10 +199,10 @@ axes1.set_ylabel('average')
199199
axes1.plot(numpy.mean(composite_data, axis=0))
200200

201201
axes2.set_ylabel('max')
202-
axes2.plot(numpy.amax(composite_data, axis=0))
202+
axes2.plot(numpy.max(composite_data, axis=0))
203203

204204
axes3.set_ylabel('min')
205-
axes3.plot(numpy.amin(composite_data, axis=0))
205+
axes3.plot(numpy.min(composite_data, axis=0))
206206

207207
fig.tight_layout()
208208

episodes/07-cond.md

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

176176
```python
177-
max_inflammation_0 = numpy.amax(data, axis=0)[0]
178-
max_inflammation_20 = numpy.amax(data, axis=0)[20]
177+
max_inflammation_0 = numpy.max(data, axis=0)[0]
178+
max_inflammation_20 = numpy.max(data, axis=0)[20]
179179

180180
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
181181
print('Suspicious looking maxima!')
@@ -186,7 +186,7 @@ the minima per day were all zero (looks like a healthy person snuck into our stu
186186
We can also check for this with an `elif` condition:
187187

188188
```python
189-
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
189+
elif numpy.sum(numpy.min(data, axis=0)) == 0:
190190
print('Minima add up to zero!')
191191
```
192192

@@ -202,12 +202,12 @@ Let's test that out:
202202
```python
203203
data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
204204

205-
max_inflammation_0 = numpy.amax(data, axis=0)[0]
206-
max_inflammation_20 = numpy.amax(data, axis=0)[20]
205+
max_inflammation_0 = numpy.max(data, axis=0)[0]
206+
max_inflammation_20 = numpy.max(data, axis=0)[20]
207207

208208
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
209209
print('Suspicious looking maxima!')
210-
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
210+
elif numpy.sum(numpy.min(data, axis=0)) == 0:
211211
print('Minima add up to zero!')
212212
else:
213213
print('Seems OK!')
@@ -220,12 +220,12 @@ Suspicious looking maxima!
220220
```python
221221
data = numpy.loadtxt(fname='inflammation-03.csv', delimiter=',')
222222

223-
max_inflammation_0 = numpy.amax(data, axis=0)[0]
224-
max_inflammation_20 = numpy.amax(data, axis=0)[20]
223+
max_inflammation_0 = numpy.max(data, axis=0)[0]
224+
max_inflammation_20 = numpy.max(data, axis=0)[20]
225225

226226
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
227227
print('Suspicious looking maxima!')
228-
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
228+
elif numpy.sum(numpy.min(data, axis=0)) == 0:
229229
print('Minima add up to zero!')
230230
else:
231231
print('Seems OK!')

episodes/08-func.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ def visualize(filename):
217217
axes1.plot(numpy.mean(data, axis=0))
218218

219219
axes2.set_ylabel('max')
220-
axes2.plot(numpy.amax(data, axis=0))
220+
axes2.plot(numpy.max(data, axis=0))
221221

222222
axes3.set_ylabel('min')
223-
axes3.plot(numpy.amin(data, axis=0))
223+
axes3.plot(numpy.min(data, axis=0))
224224

225225
fig.tight_layout()
226226
matplotlib.pyplot.show()
@@ -234,9 +234,9 @@ def detect_problems(filename):
234234

235235
data = numpy.loadtxt(fname=filename, delimiter=',')
236236

237-
if numpy.amax(data, axis=0)[0] == 0 and numpy.amax(data, axis=0)[20] == 20:
237+
if numpy.max(data, axis=0)[0] == 0 and numpy.max(data, axis=0)[20] == 20:
238238
print('Suspicious looking maxima!')
239-
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
239+
elif numpy.sum(numpy.min(data, axis=0)) == 0:
240240
print('Minima add up to zero!')
241241
else:
242242
print('Seems OK!')
@@ -317,12 +317,12 @@ It's hard to tell from the default output whether the result is correct,
317317
but there are a few tests that we can run to reassure us:
318318

319319
```python
320-
print('original min, mean, and max are:', numpy.amin(data), numpy.mean(data), numpy.amax(data))
320+
print('original min, mean, and max are:', numpy.min(data), numpy.mean(data), numpy.max(data))
321321
offset_data = offset_mean(data, 0)
322322
print('min, mean, and max of offset data are:',
323-
numpy.amin(offset_data),
323+
numpy.min(offset_data),
324324
numpy.mean(offset_data),
325-
numpy.amax(offset_data))
325+
numpy.max(offset_data))
326326
```
327327

328328
```output
@@ -779,8 +779,8 @@ then the replacement for a value `v` should be `(v-L) / (H-L)`.)
779779

780780
```python
781781
def rescale(input_array):
782-
L = numpy.amin(input_array)
783-
H = numpy.amax(input_array)
782+
L = numpy.min(input_array)
783+
H = numpy.max(input_array)
784784
output_array = (input_array - L) / (H - L)
785785
return output_array
786786
```
@@ -836,8 +836,8 @@ do the two functions always behave the same way?
836836
```python
837837
def rescale(input_array, low_val=0.0, high_val=1.0):
838838
"""rescales input array values to lie between low_val and high_val"""
839-
L = numpy.amin(input_array)
840-
H = numpy.amax(input_array)
839+
L = numpy.min(input_array)
840+
H = numpy.max(input_array)
841841
intermed_array = (input_array - L) / (H - L)
842842
output_array = intermed_array * (high_val - low_val) + low_val
843843
return output_array

episodes/10-defensive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ can you think of a function that will pass your tests but not his/hers or vice v
527527
# a possible pre-condition:
528528
assert len(input_array) > 0, 'Array length must be non-zero'
529529
# a possible post-condition:
530-
assert numpy.amin(input_array) <= average <= numpy.amax(input_array),
530+
assert numpy.min(input_array) <= average <= numpy.max(input_array),
531531
'Average should be between min and max of input values (inclusive)'
532532
```
533533

0 commit comments

Comments
 (0)