常用的数据预处理操作

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值