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.49813854431483046, SD = 0.2894614440376884
After removing extreme values:
N = 587, M = 0.5025230648640556, SD = 0.16731182277161843
You're done with this section!
Continue with numpy >>