Advertisement
Peaser

extremes

Jan 16th, 2016
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import statistics
  2.  
  3. def getExtremes(array, withIndex=False):
  4.   """
  5.  Return all extremes in an array.
  6.  An "extreme" is any value in a group that exceeds the standard deviation in the positive or negative of the mean.
  7.  withIndex returns all extremes in a tuple with their index in the array fomatted (<value>, <index>)
  8.  """
  9.   arrayMean = statistics.mean(array)
  10.   stdDev = statistics.pstdev(array)
  11.  
  12.   final = []
  13.   for item in (array if not withIndex else [(value, index) for index, value in enumerate(array)]):
  14.     item, fitem = item[0] if withIndex else item, item
  15.     if (item < arrayMean - stdDev) or (item > arrayMean + stdDev):
  16.       final.append(fitem)  
  17.   return final
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement