Yes, matrix[i, :]
will give you the i:th row of matrix
since the :
means "pick all in this dimension".
And no, A * B
is not the dot product between A
and B
, it is the element-wise product of A
and B
.
To get the dot product you would use any of
A.dot(B)
np.dot(A, B)
A @ B # Python 3.5+ only
The above is true as long as you use the np.ndarray
class, which you did if you created your matrices/arrays using np.array
, np.eye
, np.zeros
, etc.
There is also a np.matrix
class where the multiplication operator *
is actually the dot product, but it is strongly adviced to never use it since it tends to create confusion when mixed with the normal array type.
So what is going on in the expression?
Lets's break it down to parts.
multiply(colVec1,colVec2).T
will create the transpose of the element-wise product of colVec1
and colVec2
.
matrix*matrix[i,:].T
is the element-wise product between matrix
and the transpose of the i:th row of matrix
. Due to numpys broadcasting rules this is actually the same as multiplying (elementwise) each row of matrix
with its i:th row.
What we can see now is that both these expressions will create a matrix/array and not a scalar.
Therefore the call to float()
will fail, as it expects a 1-element array or scalar.
My verdict is that someone has either been using the np.matrix
class, or has interpreted the use of *
wrong.
matrix
is a regular numpy array (2d) or anp.matrix
subclass. I'm guessing the 2nd.