• Register
1 vote
372 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
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 330 views
330 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 212 views
212 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 18 views
18 views
Problem: valueerror: zero-dimensional arrays cannot be concatenated.
asked Feb 19 Dan phillip 1.9k points
0 votes
1 answer 17 views
17 views
Problem: runtimewarning: divide by zero encountered in double_scalars
asked Feb 16 Ethan ross 1.8k points
1 vote
2 answers 869 views
869 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 84 views
84 views
Problem : java does not limit the number of dimensions that an array may have
asked Nov 1, 2019 peterlaw 6.9k points
0 votes
1 answer 14 views
14 views
Problem: valueerror: all the input array dimensions except for the concatenation axis must match exactly
asked Feb 23 Muneeb Saadii 2.4k points
2 votes
2 answers 934 views
934 views
Problem: I wrote the following code in python using numpy. But it gives me some error message. The code I wrote is: import numpy as np arrayone = np.array(['Human', 'Birds', 'Groots', 'Oats']) arraytwo = np.array(['Beach', 'Toast']) arraythree ... concatenate TypeError: only integer scalar arrays can be converted to a scalar index Can anyone sort out this error and solve this. Thanks in advance.
asked May 6, 2020 samhaz 5k points