# How To Fix The NumPy Error Message: valueerror: all the input array dimensions except for the concatenation axis must match exactly

If you’re looking at this particular Numpy error, your code is probably trying to create a concatenated array using the Numpy vstack or hstack method. You’re seeing this error because at least of the input array(s) that you are feeding into the append function is of different length. And since you can’t cleanly match an array with n items with an array with n-1 items (without either dropping an array element or leaving a blank), Numpy is giving you this lovely error.

So what to do? Start by checking input array(s). At least one of them is the wrong size….

## What caused the “all dimensions must match error”?

The valueerror: all the input array dimensions except for the concatenation axis must match exactly error message occurs when creating a concatenated array from arrays of different lengths. Here is an example of the type of code it causes this problem in a numpy array, whether that is a 1d array, 2d array, or split array.

``````import numpy as np
a1 = np.array([[1a', 'b', 'c', 'd']])
a2 = np.array([[1, 2, 3]])
s3 = np.vstack((a1, a2))``````

As you can see a1 has a length of four, while a2 has a length of three. This mismatch number value in the numpy array triggers the output value error message.

The valueerror: all the input array dimensions except for the concatenation axis must match exactly error message, shows that there is a mismatch in the length of an axis in the arrays being concatenated in at least one element of your dataset or matrix. The vstack and hstack functions are special cases of the NumPy concatenate function where the concatenation axis fixed in the vertical and horizontal axis respectively. This problem occurs when the arrays being used do not have the same length for the other axis. This mismatch triggers our message because it cannot concatenate arrays of different lengths.

## How do I fix this Numpy error?

There are three ways to fix the valueerror: all the input array dimensions except for the concatenation axis must match exactly error message. The first is to change the concatenation axis.

``````import numpy as np
a1 = np.array([[1a', 'b', 'c', 'd']])
a2 = np.array([[1, 2, 3]])
s3 = np.hstack((a1, a2))``````

In this case, we fixed the problem by changing vstack to hstack, however, this changes the results. The second method is to simply make sure that the two arrays are the same length.

``````import numpy as np
a1 = np.array([[1a', 'b', 'c', 'd']])
a2 = np.array([[1, 2, 3, 4]])
s3 = np.vstack((a1, a2))``````

This code does not produce our message because they are the same length and have scalar axes of equal size. However, when you input data from a file, you may not have a choice about the existing axis length, so you have to append the shorter new array b to make them the same length.

``````a1 = np.array([[1a', 'b', 'c', 'd']])
a2 = np.array([[1, 2, 3])
l = len(a1)
a2.resize((l),refcheck=False)
s3 = np.vstack((a1, a2))``````

This automatically changes the existing axis length of a2 to the length of a1, by adding zeros to the end. These last two fixes produce a stack of the intended sequence depth with its dataframe like pattern. The last one works even when dealing with an input array or second array of equal size, where the original array is from a file.

This error message is easy to understand and easy to fix. The goal of all three fixes is to make sure the non-concatenated dimension of the arrays has the same lengths. Now that you know this error and its fixes, you can avoid the problem completely.

How To Fix The NumPy Error Message: valueerror: all the input array dimensions except for the concatenation axis must match exactly
Scroll to top