Python numpy逐行除以总和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16202348/
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-18 22:01:00  来源:igfitidea点击:

numpy divide row by row sum

pythonmultidimensional-arraynumpy

提问by Stefan Profanter

How can I divide a numpy array row by the sum of all values in this row?

如何将 numpy 数组行除以该行中所有值的总和?

This is one example. But I'm pretty sure there is a fancy and much more efficient way of doing this:

这是一个例子。但我很确定有一种奇特且更有效的方法可以做到这一点:

import numpy as np
e = np.array([[0., 1.],[2., 4.],[1., 5.]])
for row in xrange(e.shape[0]):
    e[row] /= np.sum(e[row])

Result:

结果:

array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

采纳答案by DSM

Method #1: use None(or np.newaxis) to add an extra dimension so that broadcasting will behave:

方法 #1:使用None(或np.newaxis) 添加一个额外的维度,以便广播的行为:

>>> e
array([[ 0.,  1.],
       [ 2.,  4.],
       [ 1.,  5.]])
>>> e/e.sum(axis=1)[:,None]
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

Method #2: go transpose-happy:

方法#2:转置快乐:

>>> (e.T/e.sum(axis=1)).T
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

(You can drop the axis=part for conciseness, if you want.)

axis=为了简洁起见,您可以删除该部分,如果您愿意。)

Method #3: (promoted from Jaime's comment)

方法 #3:(从 Jaime 的评论中提升)

Use the keepdimsargument on sumto preserve the dimension:

使用keepdims参数 onsum保留维度:

>>> e/e.sum(axis=1, keepdims=True)
array([[ 0.        ,  1.        ],
       [ 0.33333333,  0.66666667],
       [ 0.16666667,  0.83333333]])

回答by Ali

You can do it mathematically as enter image description here.

您可以在数学上将其作为在此处输入图片说明.

Here, Eis your original matrix and Dis a diagonal matrix where each entry is the sum of the corresponding row in E. If you're lucky enough to have an invertible D, this is a pretty mathematically convenient way to do things.

这里,E是您的原始矩阵,D是一个对角矩阵,其中每个条目是 中相应行的总和E。如果你有幸拥有一个 invertible D,这是一种在数学上非常方便的做事方式。

In numpy:

在 numpy 中:

import numpy as np

diagonal_entries = [sum(e[row]) for row in range(e.shape[0])]
D = np.diag(diagonal_entries)
D_inv = np.linalg.inv(D)
e = np.dot(e, D_inv)