# 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.4941516653877104, SD = 0.28752697514914394
After removing extreme values:
N = 596, M = 0.4866049790574771, SD = 0.16622912701103626
``````

You're done with this section!