Numpy - Python Array - Matrix Dimension
Numpy - Python Array - Matrix Dimension
1 import numpy as np
arrA = np.zeros((9000,3))
arrB = np.zerros((9000,6))
I get an error:
ValueError: all the input arrays must have same number of dimensions
I guess it's because np.shape(arrB[:,1]) is equal (9000,) instead of (9000,1) , but I cannot
figure out how to resolve it.
1 I don't really see your issue here. You have a 9000x3 and a 9000x6 array, they are different sizes, so you
cant concat them together... am I missing something? – Mo H. Apr 21, 2015 at 17:39
I want to take 9000x3 array and concatenate it with 9000x1 array getting 9000x4 array. I hope this
comment describe my issue completely. – Leopoldo Apr 21, 2015 at 17:42
Yes the dimensions/size format of the matrices is wrong they need to be equal. Try np.hstack((arrA,
np.zeros((arrayB[1], 3)))) – user3679917 Apr 21, 2015 at 17:43
https://stackoverflow.com/questions/29779401/python-array-matrix-dimension?rq=3 1/4
11/4/24, 3:54 PM numpy - Python array/matrix dimension - Stack Overflow
3 >>> arrB[:,1].shape
(9000,)
>>> arrB[:,[1]].shape
(9000, 1)
>>> out = np.hstack([arrA, arrB[:,[1]]])
>>> out.shape
(9000, 4)
Assume:
3
>>> arrA=np.arange(9000*3).reshape(9000,3)
>>> arrA
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
...,
[26991, 26992, 26993],
[26994, 26995, 26996],
[26997, 26998, 26999]])
>>> arrB=np.arange(9000*6).reshape(9000,6)
>>> arrB
array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[ 12, 13, 14, 15, 16, 17],
...,
[53982, 53983, 53984, 53985, 53986, 53987],
[53988, 53989, 53990, 53991, 53992, 53993],
[53994, 53995, 53996, 53997, 53998, 53999]])
If you take a slice of arrB, you are producing a series that looks more like a row:
>>> arrB[:,1]
array([ 1, 7, 13, ..., 53983, 53989, 53995])
What you need is a column the same shape as a column to add to arrA:
>>> arrB[:,[1]]
array([[ 1],
[ 7],
https://stackoverflow.com/questions/29779401/python-array-matrix-dimension?rq=3 2/4
11/4/24, 3:54 PM numpy - Python array/matrix dimension - Stack Overflow
[ 13],
...,
[53983],
[53989],
[53995]])
An alternate form is to specify -1 in one dimension and the number of rows or cols desired as the
other in .reshape() :
Share Edit Follow Flag edited Jan 15, 2023 at 21:12 answered Apr 21, 2015 at 18:32
Glorfindel dawg
22.6k 13 88 116 103k 23 135 214
2 np.vstack((arrA.transpose(), arrB[:,1])).transpose()
https://stackoverflow.com/questions/29779401/python-array-matrix-dimension?rq=3 3/4
11/4/24, 3:54 PM numpy - Python array/matrix dimension - Stack Overflow
It works and it's a little bit complicated. Thank you for your comment. – Leopoldo Apr 21, 2015 at
19:43
There several ways of making your selection from arrB a (9000,1) array:
2 np.hstack((arrA,arrB[:,[1]]))
np.hstack((arrA,arrB[:,1][:,None]))
np.hstack((arrA,arrB[:,1].reshape(9000,1)))
np.hstack((arrA,arrB[:,1].reshape(-1,1)))
One uses the concept of indexing with an array or list, the next adds a new axis (e.g.
np.newaxis ), the third uses reshape . These are all basic numpy array manipulation tasks.
Share Edit Follow Flag edited Apr 21, 2015 at 18:48 answered Apr 21, 2015 at 18:27
hpaulj
230k 14 253 376
https://stackoverflow.com/questions/29779401/python-array-matrix-dimension?rq=3 4/4