Normaling rows
机器学习和深度学习中经常要归一化我们的数据,因为这样做,梯度下降会更快的收敛,从而得到一个很好的效果,
归一化方法:向量中的每一个元素除以对应行每个元素的平方和。
import numpy as np
def normalizeRows(x):
"""
Implement a function that normalizes each row of the matrix x (to have unit length).
Argument:
x -- A numpy matrix of shape (n, m)
Returns:
x -- The normalized (by row) numpy matrix. You are allowed to modify x.
"""
# Compute x_norm as the norm 2 of x. Use np.linalg.norm(..., ord = 2, axis = ..., keepdims = True)
x_norm = np.linalg.norm(x,axis = 1,keepdims = True)
# Divide x by its norm.
x = x / x_norm
return x
x = np.array([
[0, 3, 4],
[1, 6, 4]])
print("normalizeRows(x) = " + str(normalizeRows(x)))