[英]Python: NumPy divide line by sum or replace it by another
i am currently trying to obtain a matrice using the NumPy library, but there is a problem that i cannot find the solution to我目前正在尝试使用 NumPy 库获取矩阵,但是有一个问题我找不到解决方案
I currently have the following numpy array我目前有以下 numpy 数组
mat =
[[0. 1. 1. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[1. 1. 0. 0. 1. 0.]
[0. 0. 0. 0. 1. 1.]
[0. 0. 0. 1. 0. 1.]
[0. 0. 0. 1. 0. 0.]]
Now i want to divide each line by the sum of the line, but if the sum of the line is equal to zero, i replace it by a line of 1/len(mat)
.现在我想将每一行除以行的总和,但如果行的总和为零,我将其替换为1/len(mat)
的行。 I tried to use the following code:我尝试使用以下代码:
for line in mat:
line /= np.sum(line)
mat[np.isnan(mat)] = 1/N
The following code will return:以下代码将返回:
[[0. 0.5 0.5 0. 0. 0. ]
[0.16666667 0.16666667 0.16666667 0.16666667 0.16666667 0.16666667]
[0.33333333 0.33333333 0. 0. 0.33333333 0. ]
[0. 0. 0. 0. 0.5 0.5 ]
[0. 0. 0. 0.5 0. 0.5 ]
[0. 0. 0. 1. 0. 0. ]]
This is the correct result i was trying to get, but it gives RuntimeWarning: invalid value encountered in true_divide
.这是我试图获得的正确结果,但它给出了RuntimeWarning: invalid value encountered in true_divide
。
So I was wondering if there was a better way to get the correct result, so i don't have to check after each line if the line if full of nan
.所以我想知道是否有更好的方法来获得正确的结果,所以我不必在每一行之后检查该行是否充满nan
。 (Also if there is a way to instantly divide each line by the sum of each line, instead of have to use the for
loop.) (此外,如果有一种方法可以立即将每行除以每行的总和,而不必使用for
循环。)
Divide by the sum row-wise and fill NaN
with 1/len(mat)
逐行除以总和并用1/len(mat)
填充NaN
np.nan_to_num( # Function to replace non-finite values with given value
np.divide( # Divide function
mat, # Input array for division
mat.sum(axis=1)[:, None] # Sum across axis=1(across row) and transpose them for division
),
nan=1/len(mat) # value that will replace non-finite values
)
One-liner单线
np.nan_to_num(np.divide(mat, mat.sum(axis=1)[:, None]), nan=1/len(mat))
Output Output
[[0. 0.5 0.5 0. 0. 0. ]
[0.16666667 0.16666667 0.16666667 0.16666667 0.16666667 0.16666667]
[0.33333333 0.33333333 0. 0. 0.33333333 0. ]
[0. 0. 0. 0. 0.5 0.5 ]
[0. 0. 0. 0.5 0. 0.5 ]
[0. 0. 0. 1. 0. 0. ]]
import numpy as np
mat = np.array([[1,2,3],[4,5,6], [-1,0,1]])
s = mat.sum(axis=1, keepdims=True) # sum of each line
out = np.ones_like(mat).astype(np.float) / len(mat) # initialize with 1/N
not_zeros = s[:,0]!=0 # logical indexes of noz zero sum lines
out[not_zeros, :] = mat[not_zeros, :] / s[not_zeros] # normalize onlt the non zero sum lines
print(out)
[[0.16666667 0.33333333 0.5 ]
[0.26666667 0.33333333 0.4 ]
[0.33333333 0.33333333 0.33333333]]
Update - zero sum lines are 1/N更新 - 零和线是 1/N
are you looking for something like你在找类似的东西吗
import numpy as np
mat = np.array([[0. ,1. ,1., 0., 0., 0.],
[0., 0., 0., 0., 0. ,0.],
[1. ,1., 0., 0., 1., 0.],
[0., 0., 0., 0. ,1., 1.],
[0. ,0., 0., 1., 0., 1.],
[0. ,0. ,0. ,1. ,0., 0.]])
sum_ = np.sum(mat, axis=1)
mat[(sum_==0), :] = 1/len(mat) # handle where sum==0
mat[~(sum_==0), :] /= sum_[~(sum_==0), np.newaxis] # handle where sum not 0
mat
array([[0. , 0.5 , 0.5 , 0. , 0. ,
0. ],
[0.16666667, 0.16666667, 0.16666667, 0.16666667, 0.16666667,
0.16666667],
[0.33333333, 0.33333333, 0. , 0. , 0.33333333,
0. ],
[0. , 0. , 0. , 0. , 0.5 ,
0.5 ],
[0. , 0. , 0. , 0.5 , 0. ,
0.5 ],
[0. , 0. , 0. , 1. , 0. ,
0. ]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.