I am building 2d array from 1d arrays in numpy (Python 2.7). I am looking for most efficient way to do it. Up to date I came up with:
a=np.ones(100000)
# SUBSCRIPTING
n_dim=3
x=0
for i in xrange(0,1000):
x=np.zeros(shape=(100000,n_dim))
for j in xrange(0,n_dim):
x[:,j]=a*j
# ~1.574 s/1000 loops - joinind 3 1d arrays
# ~9.162 s/1000 loops - joinind 10 1d arrays
# STACKING
for i in xrange(0,1000):
x=a*0.
for j in xrange(1,n_dim):
x=np.vstack((x,a*j))
x=x.T
# ~1.786 s/1000 loops - joinind 3 1d arrays
# ~16.603 s/1000 loops - joinind 10 1d arrays
First method (subscripting) is the fastest I came up with and performance gain over the second method (stacking) grows with number of 1d arrays I am joining. As I need to repeat this step quite a bit, I wonder if there is something faster? I am willing to go with solution that loses in clarity if it offers significant performance boost.
Maybe I can try stacking arrays in a way that limits number of stacking operations (e.g. case of joining 4 1d arrays: first stack arrays 1 and 2, then arrays 3 and 4, and resulting arrays at the end).
My question is about efficiently building 2d array from 1d arrays. Values in the arrays I am using here are dummy. In the real application most of the values in 1d arrays I am joining will likely differ.