Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
What's the difference between these two?
And because of this difference I'm unable to stack them using
np.vstack((array2, array1))
What changes should I make to array1 shape so that I can stack them up?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/numpy/core/shape_base.py", line 230, in vstack
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
However, with a simple change, they will stack:
>>> np.vstack((x, y[:, None]))
array([[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.],
[ 0.]])
Alternatively:
>>> np.vstack((x[:, 0], y))
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
One array is 1d, the other 2d. vertical
stack requires 2 dimensions, vertical and horizontal. So np.vstack
passes each input through np.atleast_2d
:
In [82]: np.atleast_2d(x1).shape
Out[82]: (1, 10)
But now we have a (1,10) array and a (10,1) - they can't be joined in either axis.
But if we reshape x1
so it is (10,1)
, then we can join it with x2
in either direction:
In [83]: np.concatenate((x1[:,None],x2), axis=0).shape
Out[83]: (20, 1)
In [84]: np.concatenate((x1[:,None],x2), axis=1).shape
Out[84]: (10, 2)
Print out the two arrays:
In [86]: x1
Out[86]: array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [87]: x2
Out[87]:
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
How can you concatenate those two shapes without some sort of adjustment?
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.