Replace the zeros in a NumPy integer array with nan
Date : March 29 2020, 07:55 AM
To fix the issue you can do np.nan has type float: arrays containing it must also have this datatype (or the complex or object datatype) so you may need to cast arr before you try to assign this value. The error arises because the string value 'nan' can't be converted to an integer type to match arr's type. >>> arr = arr.astype('float')
>>> arr[arr == 0] = 'nan' # or use np.nan
>>> arr
array([[ nan, 1., 2.],
[ 3., 4., 5.]])
|
how to replace only zeros of a numpy array using a mask
Date : March 29 2020, 07:55 AM
To fix this issue Others have suggested logical_and, but you have objected that it involves too much copying. But first let's set up an interative case that does this In [353]: arr=np.zeros((10,10))
In [354]: arr[3:7,3:7]=1
In [355]: tups=[(slice(5),slice(5)),
(slice(0,5),slice(3,8)),
(slice(4,9),slice(1,6))]
In [356]: for i,tup in enumerate(tups):
mask1=np.logical_and(mask,arr[tup]==0)
arr[tup][mask1]=i+1
.....:
In [357]: arr
Out[357]:
array([[ 1., 1., 1., 1., 1., 2., 2., 2., 0., 0.],
[ 1., 1., 1., 1., 1., 2., 2., 2., 0., 0.],
[ 1., 1., 1., 1., 1., 2., 2., 2., 0., 0.],
[ 1., 1., 1., 1., 1., 1., 1., 2., 0., 0.],
[ 1., 1., 1., 1., 1., 1., 1., 2., 0., 0.],
[ 0., 3., 3., 1., 1., 1., 1., 0., 0., 0.],
[ 0., 3., 3., 1., 1., 1., 1., 0., 0., 0.],
[ 0., 3., 3., 3., 3., 3., 0., 0., 0., 0.],
[ 0., 3., 3., 3., 3., 3., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
In [360]: arr[tup][mask]
Out[360]:
array([ 1., 1., 1., 1., 1., 3., 3., 1., 1., 1., 3., 3., 1.,
1., 1., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.])
In [361]: arr[tup][mask1]
Out[361]: array([ 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.])
for i,tup in enumerate(tups):
arr[tup]=np.where(arr[tup]==0,i+1,arr[tup])
In [374]: %%timeit arr=np.zeros((10,10),int);arr[3:7,3:7]=1
.....: for i,tup in enumerate(tups):
arr[tup]=np.where(arr[tup]==0,i+1,arr[tup])
.....:
1000 loops, best of 3: 134 us per loop
In [375]: %%timeit arr=np.zeros((10,10),int);arr[3:7,3:7]=1
.....: for i,tup in enumerate(tups):
mask1=np.logical_and(mask,arr[tup]==0)
arr[tup][mask1]=i+1p
.....:
10000 loops, best of 3: 64.9 us per loop
|
replace zeros in numpy array with linear interpolation between its preceding and succeeding values
Date : March 29 2020, 07:55 AM
around this issue I think your implementation is a bit off. What you want is something closer to what @Thomas came up with: y = np.array([1,2,0,4,0,5,0,0,11])
idx = np.nonzero(y)
interp = interp1d(x[idx],y[idx])
x = np.arange(len(y))
ynew = interp(x)
a_ = np.zeros(interp.x[-1] + 1)
a_[interp.x] = interp.y
|
How can I replace all numbers of an array with all numbers of an other array except of the zeros (Python, numpy)?
Tag : python , By : Mihai Mocanu
Date : March 29 2020, 07:55 AM
Hope that helps I have two arrays like these: , Just use np.where() a = np.array(a)
b = np.array(b)
a = np.where(b == 0, a, b)
np.place(a, b != 0, b[b != 0])
|
Replace zeros with mean of non-zeros along an axis of array - Python / NumPy
Date : March 29 2020, 07:55 AM
To fix this issue How can I replace 0s of the first rows by mean of the remaining rows? , Just indexing should be enough for what you want: m = data[0] == 0
data[0, m] = data[1:,m].mean(0)
print(data)
array([[4, 6, 8, 9, 3, 2, 4, 4, 0],
[4, 6, 8, 9, 3, 1, 1, 4, 0],
[4, 6, 8, 9, 3, 1, 1, 4, 0]])
m = data == 0
means = np.ma.array(data, mask = m).mean(0)
data + m * means.data
array([[4., 6., 8., 9., 3., 2., 4., 4., 0.],
[4., 6., 8., 9., 3., 1., 1., 4., 0.],
[4., 6., 8., 9., 3., 1., 1., 4., 0.]])
m = data == 0
means = np.ma.array(data, mask = m).mean(1)
data + m * means.data[:,None]
array([[3.25, 3.25, 3.25, 3.25, 3. , 2. , 4. , 4. , 3.25],
[4. , 6. , 8. , 9. , 3. , 1. , 1. , 4. , 4.5 ],
[4. , 6. , 8. , 9. , 3. , 1. , 1. , 4. , 4.5 ]])
|