. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
ValueError: all the input arrays must have same number of dimensions
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “valueerror: all the input arrays must have same number of dimensions” problem, pls let me know. Here is what I do:
n_last = []
n_last = n_list_converted[:, -1]
n_lists = np.append(n_list_converted, n_last, axis=1)
print(n_last.shape, type(n_last), n_list_converted.shape, type(n_list_converted))
Thanks!
The cause:
The reason of your error is that you concatenating two arrays that have different dimensions. In particular, in your example “1 by n” and an array of length n are different.
Solution:
I suggest you using
hstack()
andvstack()
to replace. Like as below:Notice the columns “7, 15, 23, 31” are repeated and notice that I used a[:,-1:] to replace a[:,-1]. A column is created with this my version:
To replace for a row
array([7,15,23,31])
The shapes (n1,1) and (n1,1) are different. Use
[:, None]
to convert the vector into an array.Alternativ, you can also use
n_last
when you extract itTo obtain a
(20, 1)
array.