OpenSesame
Rapunzel Code Editor
DataMatrix
Support forum
Python Tutorials
MindProbe
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.49922477258010245, SD = 0.2957300965602104
After removing extreme values:
N = 570, M = 0.49780801844566896, SD = 0.1694520778973743

You're done with this section!

Continue with numpy >>