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

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

Related questions

0 votes
1 answer 38 views
38 views
Problem: valueerror: all the input array dimensions except for the concatenation axis must match exactly
asked Feb 23 Muneeb Saadii 127k points
1 vote
2 answers 509 views
509 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 9 views
9 views
Problem: I have this error, and I need to fix it, any help: the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
2 answers 1.9K views
1.9K 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
0 votes
1 answer 5 views
5 views
Problem: i got error please anyone who help me please help non-singleton dimensions of the two input arrays must match each other.
asked Mar 17 Rohit kr 15.5k points
0 votes
1 answer 26 views
26 views
Problem: I know there is a ton of these threads but all of them are for very simple cases like 3x3 matrices and things of that sort and the solutions do not even begin to apply to my situation. So I'm trying to graph G versus l1 (that's not an eleven, but an ... 250 data points. I had another user (shout out to Hugh Bothwell!) help me with an error in my code, but now another error has surfaced.
asked Jan 6 Mashhoodch 13k points
1 vote
2 answers 602 views
602 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, 2020 Gavin 15.3k points
0 votes
1 answer 44 views
44 views
Problem : Any clue: Pandas data cast to numpy dtype of object. check input data with np.asarray(data)..
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 5.7K views
5.7K views
Problem : I have the pandas data frame with some of the categorical predictors or variables as 0 & 1, and some of the numeric variables. When I fit that to a stasmodel like below : est = sm.OLS(y, X).fit() It throws the below error : Pandas data ... hundreds of variables. For that I have concatenated the 3 pandas DataFrames to come up with the final DataFrame to be used in the model building.
asked Dec 18, 2019 alecxe 7.5k points