• Register
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?

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

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

0 votes
1 answer 215 views
215 views
Problem: valueerror: all the input array dimensions except for the concatenation axis must match exactly
asked Feb 23 Muneeb Saadii 130k points
1 vote
2 answers 1.2k views
1.2k views
Problem : I am beginner to numpy. I am trying to execute my code but I am facing below error. “Valueerror: all the input arrays must have same number of dimensions”. 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.
asked Apr 4, 2020 morrisBson 3.2k points
0 votes
1 answer 122 views
122 views
Problem: I'm not sure how I got this >Linalgerror: last 2 dimensions of the array must be square.
asked May 9 Florina Gulnar 106k points
0 votes
1 answer 143 views
143 views
Problem: I want to calculate the eigenvalues and eigenvectors w, v = numpy.linalg.eig(a) How can I do this?
asked May 2 Humaira ahmed 50.7k points
0 votes
1 answer 37 views
37 views
Problem: Axis 1 is out of bounds for array of dimension 1.
asked Jul 1 akhi 125k points
0 votes
1 answer 10 views
10 views
Problem : Axis -1 is out of bounds for array of dimension 0
asked Jun 29 Siam55 110k points
0 votes
1 answer 74 views
74 views
Problem: Is there any way around this : Axiserror: axis 1 is out of bounds for array of dimension 1?
asked Jun 23 Nahil 128k points
0 votes
1 answer 17 views
17 views
Problem: Typeerror: invalid dimensions for image data
asked Jul 10 Shifa 18.2k points
0 votes
1 answer 54 views
54 views
Problem: Index 0 is out of bounds for axis 0 with size 0
asked Jul 5 nila 121k points
0 votes
1 answer 14 views
14 views
Problem: Index 1 is out of bounds for axis 1 with size 1.
asked Jul 4 Aurelia Maja 78.4k points