How to create a function (Iteration/Recursion) to run over a dictionary of tuples in Python? -
i have python dictionary of lists one:
d = {'a': [(4, 4, 3), [1, 2, 3, 4, 5]], 'b': [(2, 1, 2), [5, 4, 3, 2, 1]], 'c': [(4, 1, 1), [2, 4, 1, 2, 4]]}
i need create formula accesses elements of dictionary and, every value [t, l]
:
t
(let's phone call m
); takes random sample s
, replacement , of length len(t)
, l
; compares m
mean of s
- true
if m
greater mean of s
, false
otherwise; repeats process 10,000 times returns percentage of times m
greater mean of s
. the output should like:
in [16]: test(d) out[16]: {'a': 0.5, 'b': 0.9, 'c': 0.4}
i think i'm not far answer, have tried:
def test(dict): def mean_diff(dict): k, (v0, v1) in dict.iteritems(): m = np.mean(v0) > (np.mean(npr.choice(v1, size=(1, len(v0)), replace=true))) homecoming ({k: m}) k, (v0, v1) in dict.iteritems(): bootstrap = np.array([means_diff(dict) _ in range(10000)]) rank = float(np.sum(bootstrap))/10000 homecoming ({k: rank})
however, got:
runtimeerror: maximum recursion depth exceeded while calling python object
i'd utilize list comprehension selects random value , compares mean. produce list of true/false. if take mean of that, averaging list of 1's , 0's, give aggregate probability.
import numpy np d = {'a': [(4, 4, 3), [1, 2, 3, 4, 5]], 'b': [(2, 1, 2), [5, 4, 3, 2, 1]], 'c': [(4, 1, 1), [2, 4, 1, 2, 4]]} def makeranks(d): rankdict = {} key in d: tup = d[key][0] mean = np.mean(tup) l = d[key][1] rank = np.mean([mean > np.mean(np.random.choice(l,len(tup))) _ in range(10000)]) rankdict[key] = rank homecoming rankdict
testing
>>> makeranks(d) {'c': 0.15529999999999999, 'a': 0.72130000000000005, 'b': 0.031899999999999998}
python function recursion dictionary iteration
No comments:
Post a Comment