在numpy数组中设置空值 [英] setting null values in a numpy array

查看:559
本文介绍了在numpy数组中设置空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据条件使numpy数组中的某些值无效? 我不明白为什么我会以0而不是不满足条件的null或空值结尾... b是一个用0和1值填充的numpy数组,c是另一个完全填充的numpy数组.所有阵列均为71x71x166

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

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

要澄清的完整问题:
我从形状为(71,71,166)的浮点填充数组开始
我基于应用于float数组的截断值创建了一个int数组,基本上创建了一些bin,大致标记出数组中的10个区域,其中介于0之间的值为0.
我要结束的是一个形状为(71,71,166)的数组,该数组在某个"bin"的特定数组方向(假设垂直方向,如果您将3D数组视为3D立方体)中具有平均值...
所以我试图遍历"bins" b == 1,b == 2等,在满足该条件的情况下对浮点数进行采样,但在其他地方为空,因此我可以取平均值,然后最后重组为一个数组循环....
不知道我是否让自己明白了.我正在使用np.where并使用索引,因为在尝试这样做时不断出错,尽管感觉效率很低.

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.

推荐答案

请考虑以下示例:

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

无论mask何时为0,数据都将设置为nan.当然,您可以使用所需的任何类型的条件,或者对b中的不同值执行不同的操作.

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.

要擦除除特定垃圾箱以外的所有内容,请尝试以下操作:

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

c[b!=1] = np.NaN

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

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

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

要获取箱中所有内容的平均值:

To get the average of everything in a bin:

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

因此,这也许可以满足您的要求(其中bins是bin值的列表):

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

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

这篇关于在numpy数组中设置空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆