Skip to content

Commit

Permalink
Merge pull request #995 from henryiii/henryiii/fix/anumpy
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
noatgnu authored Mar 21, 2023
2 parents 2157d89 + 4b95a20 commit 27315f7
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 77 deletions.
21 changes: 11 additions & 10 deletions _episodes/02-numpy.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ keypoints:
- "Use `array[x, y]` to select a single element from a 2D array."
- "Array indices start at 0, not 1."
- "Use `low:high` to specify a `slice` that includes the indices from `low` to `high-1`."
- "Use `numpy.mean(array)`, `numpy.max(array)`, and `numpy.min(array)` to calculate simple statistics."
- "Use `# some kind of explanation` to add comments to programs."
- "Use `numpy.mean(array)`, `numpy.amax(array)`, and `numpy.amin(array)` to calculate simple statistics."
- "Use `numpy.mean(array, axis=0)` or `numpy.mean(array, axis=1)` to calculate statistics across the specified axis."
---

Expand Down Expand Up @@ -357,16 +358,16 @@ We'll also use multiple assignment,
a convenient Python feature that will enable us to do this all in one line.

~~~
maxval, minval, stdval = numpy.max(data), numpy.min(data), numpy.std(data)
maxval, minval, stdval = numpy.amax(data), numpy.amin(data), numpy.std(data)
print('maximum inflammation:', maxval)
print('minimum inflammation:', minval)
print('standard deviation:', stdval)
~~~
{: .language-python}

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

~~~
maximum inflammation: 20.0
Expand Down Expand Up @@ -400,7 +401,7 @@ then ask it to do the calculation:

~~~
patient_0 = data[0, :] # 0 on the first axis (rows), everything on the second (columns)
print('maximum inflammation for patient 0:', numpy.max(patient_0))
print('maximum inflammation for patient 0:', numpy.amax(patient_0))
~~~
{: .language-python}

Expand All @@ -413,7 +414,7 @@ We don't actually need to store the row in a variable of its own.
Instead, we can combine the selection and the function call:

~~~
print('maximum inflammation for patient 2:', numpy.max(data[2, :]))
print('maximum inflammation for patient 2:', numpy.amax(data[2, :]))
~~~
{: .language-python}

Expand All @@ -428,7 +429,7 @@ diagram on the right)? As the diagram below shows, we want to perform the
operation across an axis:

![Per-patient maximum inflammation is computed row-wise across all columns using
numpy.max(data, axis=1). Per-day average inflammation is computed column-wise across all rows using
numpy.amax(data, axis=1). Per-day average inflammation is computed column-wise across all rows using
numpy.mean(data, axis=0).](../fig/python-operations-across-axes.png)

To support this functionality,
Expand Down Expand Up @@ -747,11 +748,11 @@ which is the average inflammation per patient across all days.
> it matter if the change in inflammation is an increase or a decrease?
>
> > ## Solution
> > By using the `numpy.max()` function after you apply the `numpy.diff()`
> > By using the `numpy.amax()` function after you apply the `numpy.diff()`
> > function, you will get the largest difference between days.
> >
> > ~~~
> > numpy.max(numpy.diff(data, axis=1), axis=1)
> > numpy.amax(numpy.diff(data, axis=1), axis=1)
> > ~~~
> > {: .language-python}
> >
Expand All @@ -774,7 +775,7 @@ which is the average inflammation per patient across all days.
> > between readings.
> >
> > ~~~
> > numpy.max(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
> > numpy.amax(numpy.absolute(numpy.diff(data, axis=1)), axis=1)
> > ~~~
> > {: .language-python}
> >
Expand Down
22 changes: 11 additions & 11 deletions _episodes/03-matplotlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ the medication takes 3 weeks to take effect. But a good data scientist doesn't
average of a dataset, so let's have a look at two other statistics:

~~~
max_plot = matplotlib.pyplot.plot(numpy.max(data, axis=0))
max_plot = matplotlib.pyplot.plot(numpy.amax(data, axis=0))
matplotlib.pyplot.show()
~~~
{: .language-python}

![A line graph showing the maximum inflammation across all patients over a 40-day period.](../fig/inflammation-01-maximum.svg)

~~~
min_plot = matplotlib.pyplot.plot(numpy.min(data, axis=0))
min_plot = matplotlib.pyplot.plot(numpy.amin(data, axis=0))
matplotlib.pyplot.show()
~~~
{: .language-python}
Expand Down Expand Up @@ -123,10 +123,10 @@ axes1.set_ylabel('average')
axes1.plot(numpy.mean(data, axis=0))
axes2.set_ylabel('max')
axes2.plot(numpy.max(data, axis=0))
axes2.plot(numpy.amax(data, axis=0))
axes3.set_ylabel('min')
axes3.plot(numpy.min(data, axis=0))
axes3.plot(numpy.amin(data, axis=0))
fig.tight_layout()
Expand Down Expand Up @@ -199,7 +199,7 @@ formats, including SVG, PDF, and JPEG.
> > ~~~
> > # One method
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0))
> > axes3.plot(numpy.amin(data, axis=0))
> > axes3.set_ylim(0,6)
> > ~~~
> > {: .language-python}
Expand All @@ -208,10 +208,10 @@ formats, including SVG, PDF, and JPEG.
> > ## Solution
> > ~~~
> > # A more automated approach
> > min_data = numpy.min(data, axis=0)
> > min_data = numpy.amin(data, axis=0)
> > axes3.set_ylabel('min')
> > axes3.plot(min_data)
> > axes3.set_ylim(numpy.min(min_data), numpy.max(min_data) * 1.1)
> > axes3.set_ylim(numpy.amin(min_data), numpy.amax(min_data) * 1.1)
> > ~~~
> > {: .language-python}
> {: .solution}
Expand Down Expand Up @@ -244,10 +244,10 @@ formats, including SVG, PDF, and JPEG.
> > axes1.plot(numpy.mean(data, axis=0), drawstyle='steps-mid')
> >
> > axes2.set_ylabel('max')
> > axes2.plot(numpy.max(data, axis=0), drawstyle='steps-mid')
> > axes2.plot(numpy.amax(data, axis=0), drawstyle='steps-mid')
> >
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0), drawstyle='steps-mid')
> > axes3.plot(numpy.amin(data, axis=0), drawstyle='steps-mid')
> >
> > fig.tight_layout()
> >
Expand Down Expand Up @@ -297,10 +297,10 @@ formats, including SVG, PDF, and JPEG.
> > axes1.plot(numpy.mean(data, axis=0))
> >
> > axes2.set_ylabel('max')
> > axes2.plot(numpy.max(data, axis=0))
> > axes2.plot(numpy.amax(data, axis=0))
> >
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(data, axis=0))
> > axes3.plot(numpy.amin(data, axis=0))
> >
> > fig.tight_layout()
> >
Expand Down
8 changes: 4 additions & 4 deletions _episodes/06-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ for filename in filenames:
axes1.plot(numpy.mean(data, axis=0))
axes2.set_ylabel('max')
axes2.plot(numpy.max(data, axis=0))
axes2.plot(numpy.amax(data, axis=0))
axes3.set_ylabel('min')
axes3.plot(numpy.min(data, axis=0))
axes3.plot(numpy.amin(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
Expand Down Expand Up @@ -200,10 +200,10 @@ flare-ups at all throughout the trial, suggesting that they may not even suffer
> > axes1.plot(numpy.mean(composite_data, axis=0))
> >
> > axes2.set_ylabel('max')
> > axes2.plot(numpy.max(composite_data, axis=0))
> > axes2.plot(numpy.amax(composite_data, axis=0))
> >
> > axes3.set_ylabel('min')
> > axes3.plot(numpy.min(composite_data, axis=0))
> > axes3.plot(numpy.amin(composite_data, axis=0))
> >
> > fig.tight_layout()
> >
Expand Down
18 changes: 9 additions & 9 deletions _episodes/07-cond.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ if maximum inflammation in the beginning (day 0) and in the middle (day 20) of
the study are equal to the corresponding day numbers.

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

~~~
elif numpy.sum(numpy.min(data, axis=0)) == 0:
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
print('Minima add up to zero!')
~~~
{: .language-python}
Expand All @@ -199,12 +199,12 @@ Let's test that out:
~~~
data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
max_inflammation_0 = numpy.max(data, axis=0)[0]
max_inflammation_20 = numpy.max(data, axis=0)[20]
max_inflammation_0 = numpy.amax(data, axis=0)[0]
max_inflammation_20 = numpy.amax(data, axis=0)[20]
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
print('Suspicious looking maxima!')
elif numpy.sum(numpy.min(data, axis=0)) == 0:
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
print('Minima add up to zero!')
else:
print('Seems OK!')
Expand All @@ -219,12 +219,12 @@ Suspicious looking maxima!
~~~
data = numpy.loadtxt(fname='inflammation-03.csv', delimiter=',')
max_inflammation_0 = numpy.max(data, axis=0)[0]
max_inflammation_20 = numpy.max(data, axis=0)[20]
max_inflammation_0 = numpy.amax(data, axis=0)[0]
max_inflammation_20 = numpy.amax(data, axis=0)[20]
if max_inflammation_0 == 0 and max_inflammation_20 == 20:
print('Suspicious looking maxima!')
elif numpy.sum(numpy.min(data, axis=0)) == 0:
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
print('Minima add up to zero!')
else:
print('Seems OK!')
Expand Down
22 changes: 11 additions & 11 deletions _episodes/08-func.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ def visualize(filename):
axes1.plot(numpy.mean(data, axis=0))
axes2.set_ylabel('max')
axes2.plot(numpy.max(data, axis=0))
axes2.plot(numpy.amax(data, axis=0))
axes3.set_ylabel('min')
axes3.plot(numpy.min(data, axis=0))
axes3.plot(numpy.amin(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.show()
Expand All @@ -259,9 +259,9 @@ def detect_problems(filename):
data = numpy.loadtxt(fname=filename, delimiter=',')
if numpy.max(data, axis=0)[0] == 0 and numpy.max(data, axis=0)[20] == 20:
if numpy.amax(data, axis=0)[0] == 0 and numpy.amax(data, axis=0)[20] == 20:
print('Suspicious looking maxima!')
elif numpy.sum(numpy.min(data, axis=0)) == 0:
elif numpy.sum(numpy.amin(data, axis=0)) == 0:
print('Minima add up to zero!')
else:
print('Seems OK!')
Expand Down Expand Up @@ -349,12 +349,12 @@ It's hard to tell from the default output whether the result is correct,
but there are a few tests that we can run to reassure us:

~~~
print('original min, mean, and max are:', numpy.min(data), numpy.mean(data), numpy.max(data))
print('original min, mean, and max are:', numpy.amin(data), numpy.mean(data), numpy.amax(data))
offset_data = offset_mean(data, 0)
print('min, mean, and max of offset data are:',
numpy.min(offset_data),
numpy.amin(offset_data),
numpy.mean(offset_data),
numpy.max(offset_data))
numpy.amax(offset_data))
~~~
{: .language-python}

Expand Down Expand Up @@ -825,8 +825,8 @@ readable code!
> > ## Solution
> > ~~~
> > def rescale(input_array):
> > L = numpy.min(input_array)
> > H = numpy.max(input_array)
> > L = numpy.amin(input_array)
> > H = numpy.amax(input_array)
> > output_array = (input_array - L) / (H - L)
> > return output_array
> > ~~~
Expand Down Expand Up @@ -870,8 +870,8 @@ readable code!
> > ~~~
> > def rescale(input_array, low_val=0.0, high_val=1.0):
> > """rescales input array values to lie between low_val and high_val"""
> > L = numpy.min(input_array)
> > H = numpy.max(input_array)
> > L = numpy.amin(input_array)
> > H = numpy.amax(input_array)
> > intermed_array = (input_array - L) / (H - L)
> > output_array = intermed_array * (high_val - low_val) + low_val
> > return output_array
Expand Down
2 changes: 1 addition & 1 deletion _episodes/10-defensive.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ This violates another important rule of programming:
> > # a possible pre-condition:
> > assert len(input_list) > 0, 'List length must be non-zero'
> > # a possible post-condition:
> > assert numpy.min(input_list) <= average <= numpy.max(input_list),
> > assert numpy.amin(input_list) <= average <= numpy.amax(input_list),
> > 'Average should be between min and max of input values (inclusive)'
> > ~~~
> > {: .language-python}
Expand Down
24 changes: 12 additions & 12 deletions _episodes/12-cmdline.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,11 @@ def main():
data = numpy.loadtxt(filename, delimiter=',')
if action == '--min':
values = numpy.min(data, axis=1)
values = numpy.amin(data, axis=1)
elif action == '--mean':
values = numpy.mean(data, axis=1)
elif action == '--max':
values = numpy.max(data, axis=1)
values = numpy.amax(data, axis=1)
for val in values:
print(val)
Expand Down Expand Up @@ -527,11 +527,11 @@ def process(filename, action):
data = numpy.loadtxt(filename, delimiter=',')
if action == '--min':
values = numpy.min(data, axis=1)
values = numpy.amin(data, axis=1)
elif action == '--mean':
values = numpy.mean(data, axis=1)
elif action == '--max':
values = numpy.max(data, axis=1)
values = numpy.amax(data, axis=1)
for val in values:
print(val)
Expand Down Expand Up @@ -632,11 +632,11 @@ def process(filename, action):
data = numpy.loadtxt(filename, delimiter=',')
if action == '--min':
values = numpy.min(data, axis=1)
values = numpy.amin(data, axis=1)
elif action == '--mean':
values = numpy.mean(data, axis=1)
elif action == '--max':
values = numpy.max(data, axis=1)
values = numpy.amax(data, axis=1)
for val in values:
print(val)
Expand Down Expand Up @@ -793,11 +793,11 @@ the program now does everything we set out to do.
> > data = numpy.loadtxt(filename, delimiter=',')
> >
> > if action == '-n':
> > values = numpy.min(data, axis=1)
> > values = numpy.amin(data, axis=1)
> > elif action == '-m':
> > values = numpy.mean(data, axis=1)
> > elif action == '-x':
> > values = numpy.max(data, axis=1)
> > values = numpy.amax(data, axis=1)
> >
> > for val in values:
> > print(val)
Expand Down Expand Up @@ -844,11 +844,11 @@ the program now does everything we set out to do.
> > data = numpy.loadtxt(filename, delimiter=',')
> >
> > if action == '--min':
> > values = numpy.min(data, axis=1)
> > values = numpy.amin(data, axis=1)
> > elif action == '--mean':
> > values = numpy.mean(data, axis=1)
> > elif action == '--max':
> > values = numpy.max(data, axis=1)
> > values = numpy.amax(data, axis=1)
> >
> > for val in values:
> > print(val)
Expand Down Expand Up @@ -890,11 +890,11 @@ the program now does everything we set out to do.
> > data = numpy.loadtxt(filename, delimiter=',')
> >
> > if action == '--min':
> > values = numpy.min(data, axis=1)
> > values = numpy.amin(data, axis=1)
> > elif action == '--mean':
> > values = numpy.mean(data, axis=1)
> > elif action == '--max':
> > values = numpy.max(data, axis=1)
> > values = numpy.amax(data, axis=1)
> >
> > for val in values:
> > print(val)
Expand Down
Loading

0 comments on commit 27315f7

Please sign in to comment.