Reputation: 49
I have a piece of code which involves many append
command,
items = [a1,a2,a3,a4,a5,a6,a7]
A_all = []
for t in range(len(items)):
A_BS = []
for i in range(iter):
idx_BS = np.random.choice(np.shape(items[0])[1], boots)
corr_atra = get_corr((items[t][:,idx_BS]).T)
A_BS.append(np.mean(corr_atra))
A_all.append(np.mean(A_BS))
what the code do is that for each data set a_i
bootstrap among columns and compute the correlation and then get the average there. Is there a way to avoid some of these appends?
Upvotes: 0
Views: 127
Reputation: 11351
You could pack it into list comprehensions:
s = np.shape(items[0])[1]
A_all = [
[
np.mean(get_corr(item[:, np.random.choice(s, boots)].T))
for _ in range(iter)
]
for item in items
]
But: Whatever iter
is should have another name because iter
is a built-in function.
You don't use i
- is that intensional?
Upvotes: 1