Keras拟合忽略nan值 [英] Keras fitting ignoring nan values

查看:270
本文介绍了Keras拟合忽略nan值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在训练神经网络进行回归(1个输入和1个输出).让xy分别是常用的输入和输出数据集.

I am training a neural network to do regression, (1 input and 1 output). Let's x and y be the usual input and output dataset, respectively.

我的问题是y数据集(而不是x)具有一些设置为nan的值,因此拟合到nan.我想知道是否有一个选项可以忽略拟合中的nan值,类似于numpy函数np.nanmean来计算均值忽略nans等等.

My problem is that the y dataset (not the x) have some values set to nan, so the fitting goes to nan. I wonder if there is an option to ignore the nan values in the fitting, in a similar way to the numpy functions np.nanmean to calculate the mean ignoring nans and so on.

如果该选项不存在,我想我必须找到nan值并手动擦除它们,同时擦除x中与<中的nan位置相对应的值c1>.

If that option does not exist I suppose I would have to find the nan values and erase them manually, and at the same time erase the values in x corresponding to the nan position in y.

x       y
2       4
3       2
4      np.nan
5       7
6      np.nan
7      np.nan

在这个简单的示例中,应删除y列中的nan值,同时删除x列中的相应值(4、6、7).

In this simple example the nan values in the y column should be removed and at the same time the corresponding values in the x column (4, 6, 7).

谢谢.

编辑:好的,我在过滤nan时遇到问题,我这样做了:

Ok, I have a problem filtering the nans, I do:

for index, x in np.ndenumerate(a):
    if x == np.nan:
        print index, x

它不打印任何内容,我确定有nan值...

and it doesn't print anything and I am sure there are nan values...

编辑(SELF ANSWER):好的,我找到了一种对nans进行本地化的方法:

EDIT (SELF ANSWER): Ok, I have found a way to localize the nans:

for index, x in np.ndenumerate(a):
      if x != x:
           print index, x

推荐答案

如评论中所述,只需删除nan作为预处理步骤即可:

As said in the comments, simply remove the nan as a preprocessing step:

import numpy as np

x = range(2,8)
y = [4,2,np.nan,7,np.nan,np.nan]

for a,b in zip(x,y):
    if str(b) == 'nan':
        x.remove(a)
        y.remove(b)

print x,y

产生[2, 3, 5] [4, 2, 7].

如果您正在使用某种工具来预处理提供np.nan的数据,请检查API是否允许您禁用此行为,并花点时间考虑一下这是否确实您想要的行为(或者,例如,如果您希望将此映射到常量,因为您发现您的输入即使没有标签也很有价值).

If you're using some tool to preprocess the data which gives you the np.nan, check whether the API allows you to disable this behavior and take a minute to think whether this is really the behavior you want (or if you e.g. want to map this to constants because you find your input to be valuable even though they have no labels).

这篇关于Keras拟合忽略nan值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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