All the input array dimensions except for the concatenation axis must match exactly

1 vote
360 views

Problem :

How to concatenate following numpy arrays?

First np.array with the shape (5,4) as below :

`[[  7487    500 389580      0]`
` [  7488    501 392994      0]`
` [  7491    508 389247      0]`
` [  7491    508 389247      0]`
` [  7492    502 399013      0]]`

Second np.array with the shape (1,5) as below :

`[  16.   15.   12.  12.  17. ]`

The Final result must be as shown below :

`[[  7487    500    389580    0   16]`
` [  7488    501    392994    0   15]`
` [  7491    508    389247    0   12]`
` [  7491    508    389247    0   12]`
` [  7492    502    399013    0   17]]`

I have already tried np.concatenate([array1, array2]) but i get below error

ValueError: all the input arrays must have same number of dimensions

How can I get the required output?

Solution :

You can follow the approach as given below :

`import numpy as mynp`
`x = mynp.random.randint(100, size=(5, 4))`
`y = [16, 15, 12, 12, 17]`
`print(x)`
`val = mynp.concatenate((x,mynp.reshape(y,(x.shape[0],1))),axis=1)`
`print(val)`

The output will be as below:

`[[32 37 35 53]`
` [64 23 95 76]`
` [17 76 11 30]`
` [35 42  6 80]`
` [61 88  7 56]]`
`[[32 37 35 53 16]`
` [64 23 95 76 15]`
` [17 76 11 30 12]`
` [35 42  6 80 12]`
` [61 88  7 56 17]]`
38.6k points

Related questions

215 views
Problem: valueerror: all the input array dimensions except for the concatenation axis must match exactly
1 vote
1.2k views
Problem : I am beginner to numpy. I am trying to execute my code but I am facing below error. &ldquo;Valueerror: all the input arrays must have same number of dimensions&rdquo;. I want to have my all the arrays with a same shape. I am trying to fix above error from past couple of days but still unable to fix it. I am looking for some help in fixing above error.
122 views
Problem: I'm not sure how I got this >Linalgerror: last 2 dimensions of the array must be square.
143 views
Problem: I want to calculate the eigenvalues and eigenvectors w, v = numpy.linalg.eig(a) How can I do this?
37 views
Problem: Axis 1 is out of bounds for array of dimension 1.
10 views
Problem : Axis -1 is out of bounds for array of dimension 0
74 views
Problem: Is there any way around this : Axiserror: axis 1 is out of bounds for array of dimension 1?