﻿ NumPy: Calculate the difference between the maximum and the minimum values of a given array along the second axis - w3resource # NumPy: Calculate the difference between the maximum and the minimum values of a given array along the second axis

## NumPy Statistics: Exercise-3 with Solution

Write a NumPy program to calculate the difference between the maximum and the minimum values of a given array along the second axis.

Sample Solution:-

Python Code:

``````import numpy as np
x = np.arange(12).reshape((2, 6))
print("\nOriginal array:")
print(x)
r1 = np.ptp(x, 1)
r2 = np.amax(x, 1) - np.amin(x, 1)
assert np.allclose(r1, r2)
print("\nDifference between the maximum and the minimum values of the said array:")
print(r1)
```
```

Sample Output:

```Original array:
[[ 0  1  2  3  4  5]
[ 6  7  8  9 10 11]]

Difference between the maximum and the minimum values of the said array:
[5 5]
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Python: Convert byte to a string?

You need to decode the bytes object to produce a string:

```>>> b"abcde"
b'abcde'

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> b"abcde".decode("utf-8")
'abcde'
```

Ref: https://bit.ly/2YuvClI