Normalization:向量的每个元素除以它的二范数(默认情况下)
sklearn.preprocessing.normalize(X, norm=’l2’, axis=1, copy=True)
X = [[ 1., -1., 2.],
… [ 2., 0., 0.],
… [ 0., 1., -1.]]
X_normalized = preprocessing.normalize(X, norm=’l2’)X_normalized
array([[ 0.40…, -0.40…, 0.81…],
[ 1. …, 0. …, 0. …],
[ 0. …, 0.70…, -0.70…]])
求范数
np.linalg.norm(x, ord=None, axis=None, keepdims=False)
m = np.arange(8).reshape(2,2,2)
LA.norm(m, axis=(1,2))
array([ 3.74165739, 11.22497216])
LA.norm(m[0, :, :]), LA.norm(m[1, :, :])
(3.7416573867739413, 11.224972160321824)
>>> m = np.arange(8).reshape(2,2,2)
>>> m
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> np.linalg.norm(m)
11.832159566199232
即 sqrt(1+4+9+..+79)
>>> np.linalg.norm(m, axis=2)
array([[ 1. , 3.60555128],
[ 6.40312424, 9.21954446]])
即 sqrt(0+1) sqrt(4+9)
sqrt(16+25) sqrt(56+49)
If axis is None then either a vector norm (when x is 1-D) or a matrix norm (when x is 2-D) is returned.
>>> n = np.array([1,2,3])
>>> np.linalg.norm(n)
3.7416573867739413
>>> n = np.arange(4).reshape(2,2)
>>> n
array([[0, 1],
[2, 3]])
>>> np.linalg.norm(n)
3.7416573867739413
>>> m
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> np.linalg.norm(m, axis=(1,2))
array([ 3.74165739, 11.22497216])
sqrt(1+4+9) sqrt(16+25+36+47)
>>> m
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> np.linalg.norm(m, axis=(0,1))
array([ 7.48331477, 9.16515139])
sqrt(4+16+36) sqrt(1+9+25+49)