Many times when I am working with variables, functions from different libraries, data, etc. In Python, I get errors and they are all different but very similar looking like they point to the same error:
ValueError: x and y must have same first dimension, but have shapes (10,) and (5,)
ValueError: operands could not be broadcast together with shapes (3,2) (6,)
ValueError: Dimensions must be equal, but are 1 and 64 for
ValueError: Shape of passed values is (1, 600), indices imply (2, 576)
For example, the first of them happens to me with this code from the library matplotlib
:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y = np.arange(5)
plt.plot(x, y)
As I say, the errors are relatively similar, they always talk about the dimensions or the shape (shape or dimensions).
What does this guy mean ValueError
when he always talks about shapes and dimensions? How can it be fixed?
First we have to see what a is
ValueError
, according to Python it is defined as follows:Specifically when they refer to their shape or dimensions , it means that:
The function does not accept that the variables you have passed to it have that shape, length (size), or that the operation you are trying to do is mathematically impossible.
I am going to explain it with practical examples, for this I am going to create the following variables that I will use during the answer
The library error
matplotlib
means that the functionplt()
cannot deliver two variables with different shapes (in this case length). I can replicate it by doing the following:Departure:
ValueError: x and y must have same first dimension, but have shapes (6,) and (1,)
This function requires that both the first and second variables have the same length (which is logical because if we want to draw a graph we need two variables, the "X" axis and the "Y" axis, and they have to be the same size).
Therefore we need the lists "a" (6) and "b" (1) to have the same size. I change it so that it doesn't give me any error in the output:
Indeed, the function is executed correctly, because the list "a" has a length (size) 6 and the list "b" also has a length of 6, something that the function requires.
Now let's see with some simple mathematical function, such as matrix multiplication , for this we try to multiply the matrix x with the matrix y:
Departure:
ValueError: shapes (2,6) and (3,2) not aligned: 6 (dim 1) != 3 (dim 0)
What is happening is that the operation we are trying to do is mathematically impossible. You can't multiply a (2,6) matrix with a (3,2), since the math says:
In this case the number of columns of "x" is 6 and the number of rows of "y" is 3. We could avoid the error if we use the matrix "x" together with the matrix "z" instead of with the matrix " and" .
Departure:
Conclusion
These types of errors are also seen in most Python data libraries like
Pandas
,Scipy
,matplotlib
,numpy
,stats
etc. Also especially common in neural network packages such asjax
,Tensorflow
,Pytorch
,Theano
, etc. Many times they are made by silly mistakes, like passing an empty list, passing another variable to the one you initially wanted, etc.In short , whenever a appears
ValueError
referring to shapes and dimensions . It means that the length (size) or the shape of your variables is not valid for the function you are using or is mathematically incorrect, and therefore you must change it.