0

I'm new in numpy, I understand the methods of "Joining arrays" in lower shape such as (n1, n2) beacause we can visualize, like a matrix.

But I don't undestand the logic in higher dimensions (n0, ...., n_{d-1}) of course I can't visualize that. To visualize I usually imagine a multidimensional array like a tree, so (n0, ...., n_{d-1}) means that at level (axis) i of tree every node has n_{i} children. So at level 0 (the root) we have n0 children and so on.

In substance what is the formal exact definiton of "Joining arrays" algorithms?

https://numpy.org/doc/stable/reference/routines.array-manipulation.html

4
  • What's your reference for 'joining arrays'? That's not how numpy documentation talks. Commented Mar 5, 2021 at 19:37
  • Tank you, I refer to this: numpy.org/doc/stable/reference/routines.array-manipulation.html Commented Mar 5, 2021 at 19:42
  • OK, that clairifies your question. The basic operation is concatenate which makes a new array from a list of arrays. It doesn't change the number of dimensions. The other functions tweak the dimensions in various ways before calling concatenate Commented Mar 5, 2021 at 20:02
  • It may help if you first understand numpy.org/doc/stable/reference/arrays.ndarray.html, the basics of a multi dimensional array. The join functions don't create anything new or different. Commented Mar 5, 2021 at 20:08

1 Answer 1

1

Let's see I can illustrate some basic array operations.

First make a 2d array. Start with a 1d, [0,1,...5], and reshape it to (2,3):

In [1]: x = np.arange(6).reshape(2,3)
In [2]: x
Out[2]: 
array([[0, 1, 2],
       [3, 4, 5]])

I can join 2 copies of x along the 1st dimension (vstack, v for vertical also does this):

In [3]: np.concatenate([x,x], axis=0)
Out[3]: 
array([[0, 1, 2],
       [3, 4, 5],
       [0, 1, 2],
       [3, 4, 5]])

Note that the result is (4,3); no new dimension.

Or join them 'horizontally':

In [4]: np.concatenate([x,x], axis=1)
Out[4]: 
array([[0, 1, 2, 0, 1, 2],        # (2,6) shape
       [3, 4, 5, 3, 4, 5]])

But if I supply them to np.array I make a 3d array (2,2,3) shape:

In [5]: np.array([x,x])
Out[5]: 
array([[[0, 1, 2],
        [3, 4, 5]],

       [[0, 1, 2],
        [3, 4, 5]]])

This action of np.array is really no different from making a 2d array from nested lists, np.array([[1,2],[3,4]]). We could just add a layer of nesting, just like Out[5} without the line breaks. I tend to think of this 3d array as having 2 blocks, each with 2 rows and 3 columns. But the names are just a convenience.

stack acts like np.array, making a 3d array. It actually changes the input arrays to (1,2,3) shape, and concatenates on the first axis.

In [6]: np.stack([x,x])
Out[6]: 
array([[[0, 1, 2],
        [3, 4, 5]],

       [[0, 1, 2],
        [3, 4, 5]]])

stack lets us join the array in other ways

In [7]: np.stack([x,x], axis=1)      # expand to (2,1,3) and concatante
Out[7]: 
array([[[0, 1, 2],
        [0, 1, 2]],

       [[3, 4, 5],
        [3, 4, 5]]])
In [8]: np.stack([x,x], axis=2)       # expand to (2,3,1) and concatenate
Out[8]: 
array([[[0, 0],
        [1, 1],
        [2, 2]],

       [[3, 3],
        [4, 4],
        [5, 5]]])

concatenate and the other stack functions don't add anything new to basic numpy arrays. They just provide a way(s) of making a new array from existing ones. There aren't any special algorithms.

If it helps you could think of these join functions as creating a new "blank" array, and filling it with copies of the source arrays. For example that last stack can be done with:

In [9]: res = np.zeros((2,3,2), int)
In [10]: res
Out[10]: 
array([[[0, 0],
        [0, 0],
        [0, 0]],

       [[0, 0],
        [0, 0],
        [0, 0]]])
In [11]: res[:,:,0] = x
In [12]: res[:,:,1] = x
In [13]: res
Out[13]: 
array([[[0, 0],
        [1, 1],
        [2, 2]],

       [[3, 3],
        [4, 4],
        [5, 5]]])
Sign up to request clarification or add additional context in comments.

1 Comment

In case of lower dimensione 1D, 2D, 3D is clear but in more higher dimension how can interpret or visualize concatenate?

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.