[Numpy] 对象线上填均值:numpy数组where的巧用

对象线上填均值: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,所以赋值之后改变了原数组的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值