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

1 vote
2 answers 295 views
295 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
2 answers 1.6K views
1.6K 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 2 views
2 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 6.7k points
1 vote
2 answers 295 views
295 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 8 views
8 views
Problem: I am really confused as to why this error appears. Here is my code: import numpy as np x = np.array([0, 0]) y = np.array([10, 10]) a = np.array([1, 6]) b = np.array([3, 7]) points = [x, y, a, b] max_pair = [x, y] other_pairs = ... 5, 6] not in max_pair) >>>False Why does this happen when using NumPy arrays? not in/inis ambiguous for existence? What is the correct syntax using any()\all()?
asked 14 hours ago sasha 6.4k points
0 votes
1 answer 1.8K views
1.8K 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, 2020 mphil 2.3k points
0 votes
1 answer 4.6K views
4.6K 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
0 votes
1 answer 54 views
54 views
Problem : I am new to python. I have tried to install below softwares: Python 2.5.4, Numpy 1.5.0 win32, Matplotlib 1.0.0 win32, pywin32 218. After doing so much of hard work I am still unable to plot the graphs in Python. I am facing ... > from matplotlib._path import affine_transform ImportError: DLL load failed: The specified module could not be found. Can somebody help me in fixing this error?
asked Feb 22, 2020 mphil 2.3k points
0 votes
1 answer 1.5K views
1.5K views
Problem : I am finding solution on following issue deprecationwarning: elementwise == comparison failed; this will raise an error in the future.
asked Nov 15, 2019 peterlaw 6.9k points