NaN处理 [英] NaN handling

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

问题描述

亲爱的蟒蛇专家,


我是python和这个网站的新手,所以如果这不是主题,我会道歉(即它是一个SciPy问题吗?)。我将尝试在下面演示我的问题:

--------------------------------- -----------------------

#!/ usr / local / bin / python


来自scipy import *

来自scipy.stats import *

a = norm(loc = 0,scale = 1)

a_data = a.rvs(10)


问题=零(10)

打印问题


h_x1_x2 = -sum (问题* log2(a_data))


打印h_x1_x2

#NNN

---------- ------------------------------------------------ <无线电通信/>

我需要一种处理NaN的方法,例如R有''na.omit''选项。有人知道这是否存在吗?


非常感谢


Andy


解决方案

2006-05-03,Andy McDonagh< an ********** @ earthlink.net>写道:

亲爱的python专家,

我是python和这个网站的新手,所以如果这不是主题,我道歉(即它是一个SciPy问题吗?)。我将尝试在下面展示我的问题:
------------------------------------- -------------------
#!/ usr / local / bin / python
来自scipy import *
来自scipy.stats导入*
a = norm(loc = 0,scale = 1)
a_data = a.rvs(10)

问题=零(10)打印问题

h_x1_x2 = -sum(问题* log2(a_data))

打印h_x1_x2
#NNN
------- -------------------------------------------------- -

我需要一种处理NaNs的方式


处理NaNs。


显然他们不是处理你想要的方式?

例如R有''na.omit''选项。有没有人知道这是否存在?




如果你解释你想要如何处理NaN会有所帮助,但是我会b / b
don不记得那些是任何选项。处理NaNs

除了scipy的默认方式。


-

Grant Edwards grante哇!这是没有用的......我已经去了CLUB MED了
!!

visi.com


< snip>
NaN被处理。




抛出异常会很好常规Python(非scipy)。


这可以在OSX和Linux上捕获NaN:


#假设x是一个数字

如果x + 1 == x或x!= x:

#x是NaN


但作为预防措施是昂贵的。

Assert可用于测试,如果生产代码可以用-0

或-OO运行。


< blockquote> 2006-05-05,Ivan Vinogradov< vi ***** @ mcmaster.ca>写道:

< snip>
NaNs被处理。
在常规Python(非scipy)中抛出异常会很好。




这会破坏我的大多数Python程序(至少大部分是我做浮点数的
)。我的主要问题是

NaNs(和Infs)是没有字符串表示

在各个平台上是一致的。

这个有效在OSX和Linux上捕获NaN:

#假设x是一个数字
如果x + 1 == x或x!= x:
#x是NaN
但作为预防措施是昂贵的。断言可以用于测试,如果生产代码可以用-0或
-OO运行。




有我们这些人在生产代码中需要NaNs,所以它必须是可以配置的
。我发现

在我的程序中我需要做某事的地方

例外 NaN非常有限。绝大多数时候,我需要他们悄悄地宣传。


-

格兰特爱德华兹格兰特哇!平民数千天

...已经制作了

visi.com ...感觉

美学模块 -


Dear python experts,

I am new to python and this site, so I apologize if this is off topic (i.e. is it a SciPy question?). I will try to demonstrate my problem below:
--------------------------------------------------------
#!/usr/local/bin/python

from scipy import *
from scipy.stats import *
a=norm(loc=0,scale=1)
a_data = a.rvs(10)

problem = zeros(10)
print problem

h_x1_x2 = -sum(problem * log2(a_data))

print h_x1_x2
#NaN
----------------------------------------------------------

I need a way of handling NaNs for example R has the ''na.omit'' option. Does anybody know if this exists?

Many thanks

Andy


解决方案

On 2006-05-03, Andy McDonagh <an**********@earthlink.net> wrote:

Dear python experts,

I am new to python and this site, so I apologize if this is off topic (i.e. is it a SciPy question?). I will try to demonstrate my problem below:
--------------------------------------------------------
#!/usr/local/bin/python

from scipy import *
from scipy.stats import *
a=norm(loc=0,scale=1)
a_data = a.rvs(10)

problem = zeros(10)
print problem

h_x1_x2 = -sum(problem * log2(a_data))

print h_x1_x2
#NaN
----------------------------------------------------------

I need a way of handling NaNs
NaNs are handled.

Apparently they aren''t handled the way you want them to be?
for example R has the ''na.omit'' option. Does anybody know if
this exists?



It would help if you explain how you want NaNs handled, but I
don''t recall that tehre are any "options" for handling NaNs
other than the default way in scipy.

--
Grant Edwards grante Yow! It''s NO USE... I''ve
at gone to "CLUB MED"!!
visi.com


<snip>
NaNs are handled.



Throwing an exception would be nice in regular Python (non-scipy).

This works to catch NaN on OSX and Linux:

# assuming x is a number
if x+1==x or x!=x:
#x is NaN

But is expensive as a precautionary measure.
Assert can be used for testing, if production code can be run with -0
or -OO.


On 2006-05-05, Ivan Vinogradov <vi*****@mcmaster.ca> wrote:

<snip>
NaNs are handled.
Throwing an exception would be nice in regular Python (non-scipy).



That would break most of my Python programs (at least most of
the ones in which I do floating point). My main problem with
NaNs (and Infs) is that there isn''t a string represention that
is consistent across platforms.
This works to catch NaN on OSX and Linux:

# assuming x is a number
if x+1==x or x!=x:
#x is NaN

But is expensive as a precautionary measure. Assert can be
used for testing, if production code can be run with -0 or
-OO.



There are those of us that need NaNs in production code, so it
would have to be something that could be configured. I find
that in my programs the places where I need to do something
"exceptional" with a NaN are very limited. The vast majority
of the time, I need them to propagate quietly.

--
Grant Edwards grante Yow! Thousands of days of
at civilians... have produced
visi.com a... feeling for the
aesthetic modules --


这篇关于NaN处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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