I am new on machine learning.Using python, numpy. I need to get a dot product on a matrix with size (3, 2) and each row of a matrix with size (100, 2), which is
a = [[1, 2], [3, 4], [5, 6]]
b = [[5, 5], [6, 6], [7, 7], ...] # it has 100 row
and what i want is:
np.dot(a, b[0])
np.dot(a, b[1])
i currently have:
z = np.dot(a, b)
but the dimension doesn't match
but i cannot use loop and the code need to be vectorized.
can anyone give me some hints, thank you so much!!
CodePudding user response:
It seems you want to compute the dot product of the matrix of shape 3,2
with the transpose of the matrix of shape 100,2
You can get the transpose of a matrix m
with m.T
So what you want is:
np.dot(a, b.T)
That will give you a matrix of shape 3,100
where each column is np.dot(a,b[i])
for i = 0,...,99
CodePudding user response:
For a dot product to work, it requires the first dimension of b
to match the second dimension of a
. Thus you need to transpose b
:
np.dot(a, b.T)
Output:
array([[15, 18, 21, 24],
[35, 42, 49, 56],
[55, 66, 77, 88]])
Or a
, depending on the expected output:
>>> np.dot(b, a.T)
array([[15, 35, 55],
[18, 42, 66],
[21, 49, 77],
[24, 56, 88]])