• Register
1 vote
117 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?

6 5 3
7,540 points

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]]
9 7 4
38,600 points

Related questions

1 vote
2 answers 162 views
162 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 morrisBson 3.2k points
0 votes
2 answers 1.3K views
1.3K views
Problem : I got following rather cryptic error message: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
asked Nov 18, 2019 peterlaw 6.9k points
1 vote
2 answers 130 views
130 views
Problem: Hello guys, Well, I am working with a NumPy array after learning the array a little bit. I am trying to print a particular element from each row from a two-dimensional array. While I am trying to run the program it returns an error Indexerror: too many indices for ... ;s wrong with this and how may I get rid of this? I am looking forward to hearing back from you guys. Thanks a lot.
asked Apr 21 Gavin 15.3k points
0 votes
1 answer 1.2K views
1.2K views
Problem : Currently I am trying to learn NumPy. I am trying to execute my code but I am facing following error while trying to use my code. TypeError: Cannot cast array data from dtype('float64')            to dtype('S32') according to the rule 'safe' Please Note : My NumPy version is 1.11.0. How can I fix the above error ?
asked Feb 17 mphil 2.3k points