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

8 5 2
3,230 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution :

I know how to fix the above error so I am really trying to help you with the below listed solutions.

First solution for your problem is to append zero arrays to your every list till all of your lists have the same size. So for you considering 16 is a largest size among all of your lists then in that case you should append (1, 59) shaped arrays to your every list until you get the the size of it as 16.

Another solution for you is the concatenate your lists to the very large list and after that you need to convert the very large list into the numpy array. Also you need to use the another list just to record which of the array was originally in which of the list.

Hope above solutions helps you in fixing your error.

8 4 1
5,680 points
0 votes

The solution to this problem is that concatenate your lists to a very large list and then convert that large list into a numpy array.

Solution:

First, you have to concatenate the list. For this purpose use np.concatenate, extend the second array to 2D, and then concatenate along axis =1.

np.concatenate( ( a, b[:,None]) , axis =1)

Alternative:

Alternatively, you can use np.column_stack that take care of it;

np.column_stack((a , b))

Example:

In [84]: a

Out[84]:

array([[54, 30, 55, 12],

       [64, 94, 50, 72],

       [67, 31, 56, 43],

       [26, 58, 35, 14],

       [97, 76, 84, 52]])



In [85]: b

Out[85]: array([56, 70, 43, 19, 16])


In [86]: np.concatenate((a,b[:,None]),axis=1)

Out[86]:

array([[54, 30, 55, 12, 56],

       [64, 94, 50, 72, 70],

       [67, 31, 56, 43, 43],

       [26, 58, 35, 14, 19],

       [97, 76, 84, 52, 16]])

If b is a 1D array of datatype object with shape (1,), then most probably all of the data contained in the only one element in it. If we need to flatten out before concatenating, then we use np.concatenate on it also.

Here is an example to make it clear.

In [118]: a

Out[118]:

array([[54, 30, 55, 12],

       [64, 94, 50, 72],

       [67, 31, 56, 43],

       [26, 58, 35, 14],

       [97, 76, 84, 52]])



In [119]: b

Out[119]: array([array([30, 41, 76, 13, 69])], dtype=object)



In [120]: b.shape

Out[120]: (1,)



In [121]: np.concatenate((a,np.concatenate(b)[:,None]),axis=1)

Out[121]:

array([[54, 30, 55, 12, 30],

       [64, 94, 50, 72, 41],

       [67, 31, 56, 43, 76],

       [26, 58, 35, 14, 13],

       [97, 76, 84, 52, 69]])

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 114 views
0 votes
1 answer 917 views
917 views
Iterates over a big list of .mp3 links to get the metadata tags and save it to an Excel file. Results in this error. I appreciate any help. Thanks. #print is_connected(); # Create a Pandas dataframe from the data. df = pd.DataFrame({'Links' : lines , ... \pandas\core\frame.py", line 5457, in extract_index raise ValueError('arrays must all be same length') ValueError: arrays must all be same length
asked Oct 25, 2020 psandprop 2.4k points
1 vote
1 answer 13 views
13 views
Problem: Any one here to help me with this please? The result of the sum of the elements present in each row or column of the grid is always the same.
asked Apr 23 PkGuy 23.5k points
1 vote
1 answer 275 views
275 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, ... ([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?
asked Dec 12, 2019 alecxe 7.5k points
0 votes
1 answer 164 views
164 views
Problem: valueerror: zero-dimensional arrays cannot be concatenated.
asked Feb 19 Dan phillip 4.8k points
0 votes
1 answer 79 views
79 views
Problem: runtimewarning: divide by zero encountered in double_scalars
asked Feb 16 Ethan ross 2.7k points
1 vote
2 answers 1.6K views
1.6K views
Problem : I am beginner to Ancaconda3 and Tensorflow. I have also recently installed Ancaconda3 and Tensorflow. But if I try to import the Tensorflow in the python shell then I often face the below error: ModuleNotFoundError: No module named 'numpy.core._multiarray_umath ... also installed on my box and it can be very successfully imported in the python. Please note currently I am using Windows10.
asked Apr 22, 2020 stewart 4k points
0 votes
1 answer 14 views
14 views
Problem: Please help me … how can I solve it? valueerror: err must be [ scalar | n, nx1 or 2xn array-like ]
asked Mar 31 Ifra 34.7k points
0 votes
1 answer 6 views
6 views
Problem: I am stuck now .. need some suggestions .. please help me .. Modify the guessing-game program so that the user thinks of a number that the computer must guess.
asked Apr 21 Ifra 34.7k points