对象线上填均值:numpy数组where的巧用
原题目
大意为在对角线上填上行均值
Given the following two-dimensional array, set the diagonal element to the average of the columns in each respective row not counting the diagonal element in each row. Note that the diagonal element itself is not part of the calculation. It is just a placeholder for the result.
[[0, 3, 14, 13, 12],
[13, 0, 8, 5, 11],
[11, 11, 0, 12, 10],
[11, 12, 1, 0, 10],
[13, 12, 11, 4, 0]]
In this case, your answer should look like the following
[[ 10.5 , 3. , 14. , 13. , 12. ],
[ 13. , 9.25, 8. , 5. , 11. ],
[ 11. , 11. , 11. , 12. , 10. ],
[ 11. , 12. , 1. , 8.5 , 10. ],
[ 13. , 12. , 11. , 4. , 10. ]]
In the general case, the diagonal elements won’t necessarily be equal to zero like they are here!
代码
def average_into_diagonal(x):
'''
return average of non-diagonal elements with values filled into the diagonal
:param x: square two-dimensional input array
:type x: numpy array
:returns: numpy array
'''
import numpy as np
assert isinstance(x,np.ndarray)
assert x.ndim == 2
assert x.shape[0] == x.shape[1] # square arrays only
y = x.copy().astype(np.float32)
y[y==0] = np.nan
y[np.where(np.isnan(y))] = np.nanmean(y,axis=-1)
return y
#test
# import numpy as np
# x = np.array([[0, 3, 14, 13, 12],
# [13, 0, 8, 5, 11],
# [11, 11, 0, 12, 10],
# [11, 12, 1, 0, 10],
# [13, 12, 11, 4, 0]])
# average_into_diagonal(x)
结果
array([[ 10.5 , 3. , 14. , 13. , 12. ],
[ 13. , 9.25, 8. , 5. , 11. ],
[ 11. , 11. , 11. , 12. , 10. ],
[ 11. , 12. , 1. , 8.5 , 10. ],
[ 13. , 12. , 11. , 4. , 10. ]], dtype=float32)
总结
np.where(np.isnan(y))返回一个tuple,第一个数组为y中nan值的第一维坐标,第二个数组为第二维。
此处参考
https://www.cnblogs.com/massquantity/p/8908859.html
2. np.where(condition)
只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标 (等价于numpy.nonzero)。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。
y[np.where(np.isnan(y))]切片实则为view而非copy,所以赋值之后改变了原数组的值。