I have a 5x5 array of arrays and I'm trying to matrix multiply the transpose of one row with another row.
import numpy as np
a = np.array([1, 4, 6, 4, 1])
b = np.array([-1, -2, 0, 2, 1])
c = np.array([-1, 2, 0, -2, 1])
d = np.array([-1, 0, 2, 0, -1])
e = np.array([1, -4, 6, -4, 1])
f = np.vstack([a, b, c, d, e])
result = np.dot(f[1, :].T, f[1, :])
I assumed this would work but apparently
f[1, :].T
ends up becoming
[-1, -2, 0, 2, 1]
rather than
[[-1]
[-2]
[ 0]
[ 2]
[ 1]]
and so np.dot
treats it like a real dot product rather than doing matrix multiplication.
I found out that list slicing where one index is an integer and all others are :
s reduces the dimension by one so so the shape of f[1, :]
is not (1, 5)
but (5,)
and so transposing it does nothing.
I've been able to get it to working using f[1, :].reshape((1, 5))
but is there a better way of doing this? Am I missing a simple way of getting the transpose without having to reshape it?