Set values in numpy array to NaN by index

后端 未结 2 1349
走了就别回头了
走了就别回头了 2020-12-19 22:36

I want to set specific values in a numpy array to NaN (to exclude them from a row-wise mean calculation).

I tried

import numpy

x = nump         


        
2条回答
  •  不知归路
    2020-12-19 23:01

    nan is a floating-point value. When x is an array with integer dtype, it can not be assigned a nan value. When nan is assigned to an array of integer dtype, the value is automatically converted to an int:

    In [85]: np.array(np.nan).astype(int).item()
    Out[85]: -9223372036854775808
    

    So to fix your code, make x an array of float dtype:

    x = numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]], 
                    dtype=float)
    

    import numpy
    
    x = numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]], 
                    dtype=float)
    cutoff = [5, 7]
    for i in range(len(x)):
        x[i][0:cutoff[i]:1] = numpy.nan
     print(x)
    

    yields

    array([[ nan,  nan,  nan,  nan,  nan,   5.,   6.,   7.,   8.,   9.],
           [ nan,  nan,  nan,  nan,  nan,  nan,  nan,   0.,   1.,   0.]])
    

提交回复
热议问题