0

Python Beginner here.
After going through numpy documentation which says vstack is equivalent to concatenation along the first axis after 1-D arrays of shape (N,) have been reshaped to (1,N).

So the below code

a = np.array([[1], [2], [3]])
b = np.array([[2], [3], [4]])
np.vstack((a,b))

should be

np.concatenate((a,b),axis=0))

After reshaping all 1-D arrays from (1,) to (1,1)
a will be

[[[1]]
 [[2]]
 [[3]]]

b will be

[[[2]]
 [[3]]
 [[4]]]

So,

np.concatenate((a,b),axis=0)

should be

[[[1]]
 [[2]]
 [[3]]
 [[2]]
 [[3]]
 [[4]]]

but the result shows

[[1]
 [2]
 [3]
 [2]
 [3]
 [4]]

Is there any misinterpretation from my side? Please figure out where I am going wrong here?

Sandeep Kumar
  • 53
  • 1
  • 4
  • According to [the doc](https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html?highlight=nump%20concatenate), the function "Join a sequence of arrays along an existing axis." Where is it talking about converting each input array from `(N,)` to `(1,N)` or to `(N,1)`? – fountainhead Nov 14 '20 at 22:15
  • Your input arrays are not 1-D shape `(N,)` they are already `(N,1)`. Your code just proves what the documentation is saying. – Michael Szczesny Nov 14 '20 at 22:16
  • @fountainhead - the quoted section comes from the documentation of [`np.vstack`](https://numpy.org/doc/stable/reference/generated/numpy.vstack.html), not `np.concatenate` – Michael Szczesny Nov 14 '20 at 22:21
  • @fountainhead : I was talking about the documentation of "vstack", not "concatenate". – Sandeep Kumar Nov 15 '20 at 07:55

1 Answers1

1

Here's the code:

def vstack(tup):
    arrs = np.atleast_2d(*tup)
    if not isinstance(arrs, list):
        arrs = [arrs]
    return np.concatenate(arrs, 0)

So it just makes sure the input is a list of (atleast) 2d arrays, and does a concatenate on the first axis.

Your arrays are already 2d, so it just does

In [45]: a = np.array([[1], [2], [3]])
    ...: b = np.array([[2], [3], [4]])
In [46]: a
Out[46]: 
array([[1],
       [2],
       [3]])
In [47]: b
Out[47]: 
array([[2],
       [3],
       [4]])
In [48]: np.concatenate((a,b), axis=0)
Out[48]: 
array([[1],
       [2],
       [3],
       [2],
       [3],
       [4]])

Your 'shouldbe'

In [49]: np.concatenate((a[...,None],b[...,None]), axis=0)
Out[49]: 
array([[[1]],

       [[2]],

       [[3]],

       [[2]],

       [[3]],

       [[4]]])
In [50]: _.shape
Out[50]: (6, 1, 1)

A case where the addition of a dimension matters, changing (3,) arrays to (1,3):

In [51]: np.vstack((a.ravel(),b.ravel()))
Out[51]: 
array([[1, 2, 3],
       [2, 3, 4]])
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Thanks for the clarification.. So can i assume vstack-ing 1-D arrays is a special case where the resultant array has one higher dimension higher whereas in case of 2 or more dimensions, the resultant array retains the dimensions? – Sandeep Kumar Nov 15 '20 at 08:07
  • The key is that `atleast_2d` function (see its docs an code). `np.stack` on the other hand always adds a dimension. Again see its docs and code. – hpaulj Nov 15 '20 at 08:11