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.495494403748036, SD = 0.2876636993929795
After removing extreme values:
N = 587, M = 0.49870977091345525, SD = 0.16500046772140078