规范化矩阵 python 的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36267936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:39:42  来源:igfitidea点击:

Normalizing rows of a matrix python

pythonnumpypython-2.x

提问by Yas

Given a 2-dimensional array in python, I would like to normalize each row with the following norms:

给定 python 中的二维数组,我想使用以下规范对每一行进行标准化:

  • Norm 1: L_1
  • Norm 2: L_2
  • Norm Inf: L_Inf
  • 范数 1:L_1
  • 范数 2:L_2
  • 规范信息:L_Inf

I have started this code:

我已经开始这个代码:

from numpy import linalg as LA
X = np.array([[1, 2, 3, 6],
              [4, 5, 6, 5],
              [1, 2, 5, 5],
              [4, 5,10,25],
              [5, 2,10,25]])

print X.shape
x = np.array([LA.norm(v,ord=1) for v in X])
print x

Output:

输出:

   (5, 4)             # array dimension
   [12 20 13 44 42]   # L1 on each Row

How can I modify the code such that WITHOUT using LOOP, I can directly have the rows of the matrix normalized? (Given the norm values above)

如何修改代码,以便不使用 LOOP,我可以直接对矩阵的行进行归一化?(鉴于上面的标准值)

I tried :

我试过 :

 l1 = X.sum(axis=1)

 print l1
 print X/l1.reshape(5,1)

 [12 20 13 44 42]
 [[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

but the output is zero.

但输出为零。

回答by wim

This is the L? norm:

这是L?规范:

>>> np.abs(X).sum(axis=1)
array([12, 20, 13, 44, 42])

This is the L? norm:

这是L?规范:

>>> np.sqrt((X * X).sum(axis=1))
array([  7.07106781,  10.09950494,   7.41619849,  27.67670501,  27.45906044])

This is the L∞ norm:

这是 L∞ 范数:

>>> np.abs(X).max(axis=1)
array([ 6,  6,  5, 25, 25])

To normalise rows, just divide by the norm. For example, using L? normalisation:

要规范化行,只需除以规范。例如,使用 L? 正常化:

>>> l2norm = np.sqrt((X * X).sum(axis=1))
>>> X / l2norm.reshape(5,1)
array([[ 0.14142136,  0.28284271,  0.42426407,  0.84852814],
       [ 0.39605902,  0.49507377,  0.59408853,  0.49507377],
       [ 0.13483997,  0.26967994,  0.67419986,  0.67419986],
       [ 0.14452587,  0.18065734,  0.36131469,  0.90328672],
       [ 0.18208926,  0.0728357 ,  0.36417852,  0.9104463 ]])
>>> np.sqrt((_ * _).sum(axis=1))
array([ 1.,  1.,  1.,  1.,  1.])


More direct is the normmethod in numpy.linalg, if you have it available:

更直接的是 中的norm方法numpy.linalg,如果你有的话:

>>> from numpy.linalg import norm
>>> norm(X, axis=1, ord=1)  # L-1 norm
array([12, 20, 13, 44, 42])
>>> norm(X, axis=1, ord=2)  # L-2 norm
array([  7.07106781,  10.09950494,   7.41619849,  27.67670501,  27.45906044])
>>> norm(X, axis=1, ord=np.inf)  # L-∞ norm
array([ 6,  6,  5, 25, 25])


(after OP edit):You saw zero values because /is an integer division in Python 2.x. Either upgrade to Python 3, or change dtype to float to avoid that integer division:

(在 OP 编辑​​之后):您看到零值,因为/是 Python 2.x 中的整数除法。要么升级到 Python 3,要么将 dtype 更改为 float 以避免整数除法:

>>> linfnorm = norm(X, axis=1, ord=np.inf)
>>> X.astype(np.float) / linfnorm[:,None]
array([[ 0.16666667,  0.33333333,  0.5       ,  1.        ],
       [ 0.66666667,  0.83333333,  1.        ,  0.83333333],
       [ 0.2       ,  0.4       ,  1.        ,  1.        ],
       [ 0.16      ,  0.2       ,  0.4       ,  1.        ],
       [ 0.2       ,  0.08      ,  0.4       ,  1.        ]])

回答by ayhan

You can pass axis=1parameter:

您可以传递axis=1参数:

In [58]: LA.norm(X, axis=1, ord=1)
Out[58]: array([12, 20, 13, 44, 42])


In [59]: LA.norm(X, axis=1, ord=2)
Out[59]: array([  7.07106781,  10.09950494,   7.41619849,  27.67670501,  27.45906044])