在numpy数组中设置空值

时间:2023-02-04 21:19:50

how do I null certain values in numpy array based on a condition? I don't understand why I end up with 0 instead of null or empty values where the condition is not met... b is a numpy array populated with 0 and 1 values, c is another fully populated numpy array. All arrays are 71x71x166

如何根据条件使numpy数组中的某些值为空?我不明白为什么我最终得到0而不是null或空值不满足条件... b是一个用0和1值填充的numpy数组,c是另一个完全填充的numpy数组。所有阵列都是71x71x166

a = np.empty(((71,71,166)))
d = np.empty(((71,71,166)))
for indexes, value in np.ndenumerate(b):
    i,j,k = indexes
    a[i,j,k] = np.where(b[i,j,k] == 1, c[i,j,k], d[i,j,k])

I want to end up with an array which only has values where the condition is met and is empty everywhere else but with out changing its shape

我想最终得到一个数组,该数组只有满足条件的值,并且在其他任何地方都是空的,但不改变它的形状

FULL ISSUE FOR CLARIFICATION as asked for:
I start with a float populated array with shape (71,71,166)
I make an int array based on a cutoff applied to the float array basically creating a number of bins, roughly marking out 10 areas within the array with 0 values in between
What I want to end up with is an array with shape (71,71,166) which has the average values in a particular array direction (assuming vertical direction, if you think of a 3D array as a 3D cube) of a certain "bin"...
so I was trying to loop through the "bins" b == 1, b == 2 etc, sampling the float where that condition is met but being null elsewhere so I can take the average, and then recombine into one array at the end of the loop....
Not sure if I'm making myself understood. I'm using the np.where and using the indexing as I keep getting errors when I try and do it without although it feels very inefficient.

要求的全部澄清问题:我从一个带有形状的浮动填充数组开始(71,71,166)我根据应用于浮点数组的截止值创建一个int数组,基本上创建了一些数据库,大致标出了10个区域内的我想要最终得到的数组是一个具有形状(71,71,166)的数组,其具有特定数组方向的平均值(假设垂直方向,如果您将3D数组视为3D立方体)一个“bin”...所以我试图循环通过“bins”b == 1,b == 2等,在满足条件的情况下对float进行采样,但在其他地方为null,所以我可以取平均值,然后在循环结束时重新组合成一个数组....不确定我是否让自己理解。我正在使用np.where并使用索引,因为当我尝试执行此操作时我不断收到错误,尽管它感觉非常低效。

2 个解决方案

#1


4  

Consider this example:

考虑这个例子:

import numpy as np
data = np.random.random((4,3))
mask = np.random.random_integers(0,1,(4,3))
data[mask==0] = np.NaN

The data will be set to nan wherever the mask is 0. You can use any kind of condition you want, of course, or do something different for different values in b.

当掩码为0时,数据将设置为nan。当然,您可以使用任何类型的条件,或者对b中的不同值执行不同的操作。

To erase everything except a specific bin, try the following:

要删除除特定bin之外的所有内容,请尝试以下操作:

c[b!=1] = np.NaN

So, to make a copy of everything in a specific bin:

因此,要复制特定bin中的所有内容:

a = np.copy(c)
a[b!=1] == np.NaN

To get the average of everything in a bin:

要获得bin中所有内容的平均值:

np.mean(c[b==1])

So perhaps this might do what you want (where bins is a list of bin values):

所以也许这可能会做你想要的(其中bin是bin值的列表):

a = np.empty(c.shape)
a[b==0] = np.NaN
for bin in bins:
    a[b==bin] = np.mean(c[b==bin])

#2


2  

np.empty sometimes fills the array with 0's; it's undefined what the contents of an empty() array is, so 0 is perfectly valid. For example, try this instead:

np.empty有时用0填充数组;它未定义empty()数组的内容是什么,因此0完全有效。例如,尝试这样做:

d = np.nan * np.empty((71, 71, 166)).

But consider using numpy's strength, and don't iterate over the array:

但是考虑使用numpy的强度,而不是迭代数组:

a = np.where(b, c, d)

(since b is 0 or 1, I've excluded the explicit comparison b == 1.)

(因为b是0或1,我排除了显式比较b == 1.)

You may even want to consider using a masked array instead:

您甚至可以考虑使用掩码数组:

a = np.ma.masked_where(b, c)

which seems to make more sense with respect to your question: "how do I null certain values in a numpy array based on a condition" (replace null with mask and you're done).

对于你的问题似乎更有意义:“如何根据条件使numpy数组中的某些值为空”(用掩码替换null并完成)。

#1


4  

Consider this example:

考虑这个例子:

import numpy as np
data = np.random.random((4,3))
mask = np.random.random_integers(0,1,(4,3))
data[mask==0] = np.NaN

The data will be set to nan wherever the mask is 0. You can use any kind of condition you want, of course, or do something different for different values in b.

当掩码为0时,数据将设置为nan。当然,您可以使用任何类型的条件,或者对b中的不同值执行不同的操作。

To erase everything except a specific bin, try the following:

要删除除特定bin之外的所有内容,请尝试以下操作:

c[b!=1] = np.NaN

So, to make a copy of everything in a specific bin:

因此,要复制特定bin中的所有内容:

a = np.copy(c)
a[b!=1] == np.NaN

To get the average of everything in a bin:

要获得bin中所有内容的平均值:

np.mean(c[b==1])

So perhaps this might do what you want (where bins is a list of bin values):

所以也许这可能会做你想要的(其中bin是bin值的列表):

a = np.empty(c.shape)
a[b==0] = np.NaN
for bin in bins:
    a[b==bin] = np.mean(c[b==bin])

#2


2  

np.empty sometimes fills the array with 0's; it's undefined what the contents of an empty() array is, so 0 is perfectly valid. For example, try this instead:

np.empty有时用0填充数组;它未定义empty()数组的内容是什么,因此0完全有效。例如,尝试这样做:

d = np.nan * np.empty((71, 71, 166)).

But consider using numpy's strength, and don't iterate over the array:

但是考虑使用numpy的强度,而不是迭代数组:

a = np.where(b, c, d)

(since b is 0 or 1, I've excluded the explicit comparison b == 1.)

(因为b是0或1,我排除了显式比较b == 1.)

You may even want to consider using a masked array instead:

您甚至可以考虑使用掩码数组:

a = np.ma.masked_where(b, c)

which seems to make more sense with respect to your question: "how do I null certain values in a numpy array based on a condition" (replace null with mask and you're done).

对于你的问题似乎更有意义:“如何根据条件使numpy数组中的某些值为空”(用掩码替换null并完成)。