numpy逐行划分

时间:2022-03-13 22:29:02

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]])

2 个解决方案

#1


66  

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:go transpose-happy:

>>> (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.)

(如果需要,您可以删除轴=部分以获得简洁。)

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

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

Use the keepdims argument on sum to preserve the dimension:

在sum上使用keepdims参数来保留维度:

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

#2


5  

You can do it mathematically as numpy逐行划分.

你可以用数学方法做到这一点。

Here, E is your original matrix and D is 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中相应行的总和。如果你有幸拥有一个可逆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)

#1


66  

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:go transpose-happy:

>>> (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.)

(如果需要,您可以删除轴=部分以获得简洁。)

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

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

Use the keepdims argument on sum to preserve the dimension:

在sum上使用keepdims参数来保留维度:

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

#2


5  

You can do it mathematically as numpy逐行划分.

你可以用数学方法做到这一点。

Here, E is your original matrix and D is 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中相应行的总和。如果你有幸拥有一个可逆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)