Scipy Expit:意外行为.钠盐 [英] Scipy expit: Unexpected behavour. NaNs

查看:217
本文介绍了Scipy Expit:意外行为.钠盐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

略去了一些 nan 的内容. (并扩展并触摸他们触摸的所有内容) 做了一些仔细的调查,并给出了一个最小的工作示例:

Noticed some nan's were appearing unexpectedly, in my data. (and expanding out and naning everything they touched) Did some careful investigation and produced a minimal working example:

>>> import numpy
>>> from scipy.special import expit
>>> expit(709)
1.0
>>> expit(710)
nan

Expit是倒数logit. 此处的Scipy文档. 告诉我们: expit(x) = 1/(1+exp(-x))

Expit is the inverse logit. Scipy documentation here. Which tells us: expit(x) = 1/(1+exp(-x))

所以1+exp(-709)==1.0使得expit(709)=1.0似乎很合理,将exp(-709)==0舍入.
但是,expit(710)怎么了?
expit(710)==nan表示1+exp(-710)==0,这表示:exp(-710)=-1根本不正确.

So 1+exp(-709)==1.0 so that expit(709)=1.0 Seems fairly reasonable, rounding exp(-709)==0.
However, what is going on with expit(710)?
expit(710)==nan implies that 1+exp(-710)==0, which implies: exp(-710)=-1 which is not right at all.

发生了什么事?

我通过以下方式修复它:

I am fixing it with:

def sane_expit(x):
    x = np.minimum(x,700*np.ones_like(x)) #Cap it at 700 to avoid overflow
    return expit(x)

但这会变慢一些,因为额外的操作和python开销.

But this is going to be a bit slower, because extra op, and the python overhead.

我正在使用numpy 1.8.-0和scipy 0.13.2

I am using numpy 1.8.-0, and scipy 0.13.2

推荐答案

这是怎么回事?

What is going on?

该函数显然未进行编码以处理如此大的输入,并且在内部计算过程中遇到溢出.

The function is evidently not coded to deal with such large inputs, and encounters an overflow during the internal calculations.

数字710的含义是math.exp(709)可以表示为float,而math.exp(710)不能:

The significance of the number 710 is that math.exp(709) can be represented as float, whereas math.exp(710) cannot:

In [27]: import math

In [28]: math.exp(709)
Out[28]: 8.218407461554972e+307

In [29]: math.exp(710)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
----> 1 math.exp(710)

OverflowError: math range error

可能值得提交针对SciPy的错误.

这篇关于Scipy Expit:意外行为.钠盐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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