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 = numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]])
cutoff = [5, 7]
for i in range(len(x)):
x[i][0:cutoff[i]:1] = numpy.nan
Looking at x
, I only see -9223372036854775808
where I expect NaN
.
I thought about an alternative:
for i in range(len(x)):
for k in range(cutoff[i]):
x[i][k] = numpy.nan
Nothing happens. What am I doing wrong?
nan
is a floating-point value. Whenx
is an array with integer dtype, it can not be assigned a nan value. Whennan
is assigned to an array of integer dtype, the value is automatically converted to an int:So to fix your code, make
x
an array of float dtype:yields
Vectorized approach to set appropriate elements as NaNs
@unutbu's solution must get rid of the value error you were getting. If you are looking to
vectorize
for performance, you can useboolean indexing
like so -Sample run -
Vectorized approach to directly calculate row-wise mean of appropriate elements
If you were trying to get the masked mean values, you can modify the earlier proposed vectorized approach to avoid dealing with
NaNs
altogether and more importantly keepx
with integer values. Here's the modified approach -Here's a sample run for such a solution -