StatsSorceress
StatsSorceress

Reputation: 3099

Python numpy sum (-1)

Suppose I want to normalize a matrix A. I came across this code:

A_norm = (A / np.sqrt((A ** 2).sum(-1))[..., np.newaxis]).astype(np.float32)

We're subtracting a mean of 0 here, I assume. My problem is the denominator. We're taking the square root of something we've squared and summed, but I don't understand what.

Specifically, what does this do:

np.sqrt((A ** 2).sum(-1))[..., np.newaxis]

Upvotes: 3

Views: 5881

Answers (1)

keepAlive
keepAlive

Reputation: 6665

We have

np.sqrt((A ** 2).sum(-1))

which can be decomposed as

B = A**2
C = B.sum(-1)
D = np.sqrt(C)

where C is the row-sum of B, which has been flatten by the operation (the column sum would have been B.sum(0)) and D is the entrywise squareroot of C.

For A to be correctly normalized, we need the denominator to be correctly shaped, so as to divide the kth row of A by the kth term of D. Thus we must explicitly reshape the flatten result of np.sqrt(C) as being a column vector as follows

D = np.sqrt(C)[..., np.newaxis]

Upvotes: 3

Related Questions