4 ms·
It's not historical. It's conceptual and deliberate (and far less confusing than the alternative, i.m.o.). What's likely happening is that you're using a 1D ar
by jofer 10y ago
It's not historical. It's conceptual and deliberate (and far less confusing than the alternative, i.m.o.).
What's likely happening is that you're using a 1D array and a 2D array. If they were both actually vectors, `x.dot(y)` would work fine.
For example:
import numpy as np
x = y = np.arange(10)
x.dot(y) # Yields 285
However, your problem is likely that one's a vector and one's a 2D array. Matlab doesn't actually have vectors at all, so this distinction confuses a lot of people. A row vector and a column vector are both 2D and aren't actually vectors at all. You can transpose them and swap the dimensions. When you transpose a vector in numpy, you still get a 1D vector. Transposing doesn't change the number of dimensions (To me it would be _incredibly_ confusing if it did...).
So, in your case, you probably had something like:
import numpy as np
x = np.arange(10)
y = x[np.newaxis, :]
x.dot(y) # Raises a ValueError
Note that the opposite (`y.dot(x)`) would have worked fine in this case, as well as `np.inner`.
`np.dot` uses the last dimension of the first array (10) and the second-to-last dimension of the other array (1). They don't match. It's that simple.
You might have also been trying to take the dot product of two 2D arrays with the same dimension. In that case, the same thing happens. The last dimension of one doesn't match the second-to-last dimension of the other.
- tomrod 10y agoVery thorough explanation!