python - normalizing a matrix in numpy -
what np.linalg.norm(x, axis = 1, keepdims=true) return?
i have matrix np.array([[3.0,4.0],[1, 2]]) . trying normalize each row of matrix . answer should np.array([[0.6,0.8],[0.4472136,0.89442719]]) not able understand code answer.
here code:
x = np.array([[3.0,4.0],[1, 2]]) norms = np.linalg.norm(x, axis = 1, keepdims = true) x /= norms this code should give normalized x don't understand np.linalg.norm() returning here.
np.linalg.norm(x, axis = 1, keepdims=true) doing in every row (for x):
np.sqrt(3**2 + 4**2) row 1 of x gives 5
np.sqrt(1**2 + 2**2) row 2 of x gives 2.23
this vector [5, 2.23] norms variable
all values in x divided norms variable should give np.array([[0.6,0.8],[0.4472136,0.89442719]]). hope helps
Comments
Post a Comment