SHARE
TWEET

Find median or any statistic parameter in loop by array key

a guest Nov 14th, 2012 7 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3.  
  4. def monthlyMediana(array, length, whichElementIsValue=1, whichElementIsKey=0, startKey=1):
  5.     tmpMonthArray=[[]*length for x in xrange(length)] # how long the array is? (if calculate over months, there are 12 month and then value is 12)
  6.     for liste in array:                               # cycle to reconfiguring array "by key"
  7.         for each in liste:                            # inner cycle to get out values from array  
  8.             try:
  9.                 tmpMonthArray[each[whichElementIsKey]-startKey].append(each[whichElementIsValue]) #be careful with 'each[0]-startKey' By default the first element = 1, but its index in tmpArray is 0. So to correct it, subtracting helps to get correct index.
  10.             except Exception:
  11.                 print ("Check input array in function 'monthlyMediana'! There is no value in defined element '" + str(whichElementIsValue)+"'")
  12.                 raise
  13.            
  14.     i=1
  15.     resultArray=[]
  16.     for each in tmpMonthArray:                      # cycle through reconfigured array to calculate median by key array
  17.         resultArray.append([i,np.median(each)])
  18.         i+=1
  19.     return resultArray
  20.  
  21.  
  22.  
  23. # testing function calculating median from two arrays. the first one-
  24. # array1 is manually created, by hand, consists of pairs - key and value
  25. array1=[[1, 83.08781741696035], [2, 83.00913411194273], [3, 82.87611049475012], [4, 81.9209957077651], [5, 83.29382676081318], [6, 86.43752414116936], [7, 90.28708527096674], [8, 85.4778304673852], [9, 81.50339901314508], [10, 82.86330480401524], [11, 81.2255531150904], [12, 81.61939222634093]]
  26. i=1
  27.  
  28. # the second array2 is synthetically generated in next loop and it will consist half the values of array1, keeping the same keys
  29. array2=[]
  30. for each in array1:
  31.     tmp=[i,each[1]/2]
  32.     array2.append(tmp)
  33.     i+=1
  34.  
  35. #lets check them
  36. print "array 1", array1
  37. print ""
  38. print "array 2", array2
  39. print""
  40. #now we need to combine both arrays into array, which can be given to monthlyMediana function.
  41. # it is like a wrapeer, wraps both small arrays.
  42. wrapperArray=[]
  43. wrapperArray.append(array1)
  44. wrapperArray.append(array2)
  45.  
  46. #check out the configuration of such wrapper array. simple, huh?
  47. print "wrapper array", wrapperArray
  48. print""
  49.  
  50.  
  51. length=len(array1) #how many keys we have?
  52. print "how many keys we have?", length
  53. print ""
  54.  
  55. print "calculated medianas by key", monthlyMediana(wrapperArray,length,1,0,1)
  56. print""
  57. print "calculated medianas by key with default whichElementIsValue=1, whichElementIsKey=0, startKey=1", monthlyMediana(wrapperArray,length)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top