0
float(multiply(colVec1,colVec2).T * (matrix*matrix[i,:].T)) + c 

I am new to Python and numpy and I am trying to understand what the code snippet above does.

The multiply().T part performs an element-by-element multiplication and then does a transpose, and so the result becomes a row vector.

I am trying to understand what matrix[i,:] does. Does it create a sub-matrix by picking just the i'th row vector or does it create a sub-matrix from the i'th row vector all the way to the end of the matrix?

The * performs a dot-product which is then converted to a float using float().

1
  • A crucial question is whether matrix is a regular numpy array (2d) or a np.matrix subclass. I'm guessing the 2nd.
    – hpaulj
    Commented Sep 2, 2016 at 13:57

1 Answer 1

1

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.

7
  • Okay, so what exactly is the code snippet in the question doing? The result of everything in float() is a scalar and I am trying to understand how :)
    – An SO User
    Commented Sep 2, 2016 at 9:51
  • I have updated the answer. Short answer: the code should not work (in general) as the argument to float() will not be a scalar. Commented Sep 2, 2016 at 10:02
  • 1
    Also, in the future, if you are unsure about what a piece of code does, the easiest way to find out is to simply run it in the interpreter. Commented Sep 2, 2016 at 10:02
  • So if it is np.matrix class, the * becomes a dot product and thus a scalar in float().
    – An SO User
    Commented Sep 2, 2016 at 10:07
  • 1
    No. Which you can easily verify by running the code yourself. Although the left colVec part is (supposedly) an array, the multiplication with the right matrix part will be executed as the dot product since one part is a matrix type. All this is actually a stellar showcase of why the np.matrix classes should be avoided. Commented Sep 2, 2016 at 11:07

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.