0

I tried a lot but none of the concatenate or vstack works for me.

2 Answers 2

5

Have you tried np.array?

np.array([[1,2],[3,4]])

makes a 2d array by concatenating 2 1d arrays (lists)

Similarly

np.array([np.ones(3,3), np.zeros(3,3)]]

should produce a (2,3,3) array.

An newish np.stack function gives you more control over which axis is added. It works by expanding the dimensions of all input arrays by one, and concatenating.

You can expand the dimensions yourself, e.g.

In [378]: A=np.ones((2,3),int)
In [379]: B=np.zeros((2,3),int)
In [380]: np.concatenate([A[None,:,:], B[None,:,:]], axis=0)
Out[380]: 
array([[[1, 1, 1],
        [1, 1, 1]],

       [[0, 0, 0],
        [0, 0, 0]]])
In [381]: _.shape
Out[381]: (2, 2, 3)

The key things to understand are:

  • matching dimensions of the inputs - they have to match on all but the dimension that is being joined

  • expanding the dimensions of inputs as needed. To concatenate 2d arrays to form a 3d, the 2d's have to expand to 3d first. That None or np.newaxis trick is especially valuable.

  • concatenate along the right axis.

stack, hstack, vstack etc all facilitate this, but a skill numpy user should be able to work directly with concatenate. Practice with small samples in an interactive session.

In [385]: np.array((A,B)).shape
Out[385]: (2, 2, 3)
In [386]: np.stack((A,B)).shape
Out[386]: (2, 2, 3)
In [387]: np.stack((A,B),axis=1).shape
Out[387]: (2, 2, 3)
In [388]: np.stack((A,B),axis=2).shape
Out[388]: (2, 3, 2)

If the arrays differ in shape, np.array will create an object dtype array

In [389]: C=np.ones((3,3))
In [390]: np.array((A,C))
Out[390]: 
array([array([[1, 1, 1],
       [1, 1, 1]]),
       array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])], dtype=object)
In [391]: _.shape
Out[391]: (2,)

dstack (and stack) will have problems with different size arrays:

In [392]: np.dstack((A,B,C))
....
ValueError: all the input array dimensions except for the concatenation axis must match exactly
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried np.array() but that returns me a numpy array with shape (n,), while each element within that is another numpy array.
Then your n arrays must differ in shape. It is creating an object array. What's the shape of your arrays? I don't see how dstack would work.
I didn't realize that it at that time! I finally found some of my numpy arrays are not with the same size. Thanks for your answer so much!
2

You could use np.dstack, documentation can be located here: https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.dstack.html

import numpy as np

l1 = []
# create list of arrays
for i in range(5):
    l1.append(np.random.random((5, 3)))

# convert list of arrays into 3-dimensional array
d = np.dstack(l1)

d.shape #(5,3,5)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.