I have a dictionary that is keyed by date and filled with classes that have an attribute that is a numpy.array. I want to use np.dstack to make one large array from all the arrays in the dictionary. My current code is like this:
import numpy as np
#PARTS is my dictionary
#the .partposit is the attribute that is an array of shape (50000, 12)
ks = sorted(PARTS.keys())
p1 = PARTS[ks[0]].partposit
for k in ks[1:]:
p1 = np.dstack((p1, PARTS[k].partposit))
My result is as I expect:
In [67]: p1.shape
Out[67]: (50000, 12, 163)
However, it is quite slow. Is there a more efficient way to do this?
you could try this:
it took a few seconds to stack it on my machine.
numpy.dstacktakes in a sequence of arrays and stacks them together as such it would be much faster if we just give it the list instead of continuously stacking them ourselves.http://docs.scipy.org/doc/numpy/reference/generated/numpy.dstack.html
I was also curious as to see how long your method would be:
ouch!
I hope this helps.