Python videos

# NumPy: Exercises and Solutions

## Removing extreme values

### Exercise

• Generate an array of a thousand random numbers
• Print out the shape of the array, and the mean and standard deviation of these numbers
• Remove all numbers that are more than one standard deviation above or below the mean
• Again, print out the shape of the array, and the mean and standard deviation of the remaining numbers

### Solution

``````import numpy as np

a = np.random.random(1000)
m = np.mean(a)
sd = np.std(a)
print('Before removing extreme values:')
print('N = {}, M = {}, SD = {}'.format(len(a), m, sd))
new_a = a[(a > m - sd) & (a < m + sd)]
new_m = np.mean(new_a)
new_sd = np.std(new_a)
print('After removing extreme values:')
print('N = {}, M = {}, SD = {}'.format(len(new_a), new_m, new_sd))
``````

Output:

``````Before removing extreme values:
N = 1000, M = 0.47584620688058077, SD = 0.2872202276618915
After removing extreme values:
N = 575, M = 0.4660418590730324, SD = 0.16177725026674805
``````

You're done with this section!