numpy.isfinite() 中出现意外异常
我遇到了一个我不太明白的异常。我的np.array v的来源比较复杂,但在这里是发生异常时的代码:
print v, type(v)
for val in v:
print val, type(val)
print "use isfinte() with astype(float64): "
np.isfinite(v.astype("float64"))
print "use isfinite() as usual: "
try:
np.isfinite(v)
except Exception,e:
print e
这段代码的输出是:
[6.4441947744288255 7.2246449651781788 4.1028442021807656
4.8832943929301189] <type 'numpy.ndarray'>
6.44419477443 <type 'numpy.float64'>
7.22464496518 <type 'numpy.float64'>
4.10284420218 <type 'numpy.float64'>
4.88329439293 <type 'numpy.float64'>
np.isfinte() with astype(float64):
[ True True True True]
np.isfinte() as usual:
ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
我不明白这个TypeError(类型错误)。所有元素都是np.float64类型,应该没问题。可能是个bug?这个错误只是偶尔出现,但我找不到数组之间的区别。它们的类型总是一样的。
提前谢谢你们。
编辑:工作示例:
数据结构像上面显示的那么小。
import pandas as pd
import numpy as np
def forward_estim(H,end):
old_idx = H.index
new_idx = pd.period_range(old_idx[-1],end,freq=old_idx.freq)
H_estim = pd.DataFrame(columns=["A","B","C","D"],index=new_idx)
H_chg = H.values[1:]-H.values[:-1]
mean_ = H_chg.mean()
std_ = H_chg.std()
H_estim.ix[0] = H.ix[-1]
for i in range(1,len(H_estim)):
H_estim.A[i] = H_estim.A[i-1] + mean_ + std_/2
H_estim.B[i] = H_estim.B[i-1] + mean_ + std_
H_estim.C[i] = H_estim.C[i-1] + mean_ - std_
H_estim.D[i] = H_estim.D[i-1] + mean_ - std_/2
return H_estim.ix[1:]
H_idx = pd.period_range("2010-01-01","2012-01-01",freq="A")
print H_idx
H = pd.Series(np.array([2.3,3.0,2.9]),index=H_idx)
print H
H_estim = forward_estim(H,"2014-01-01")
print H_estim
np.isfinite(H_estim.values.astype("float64"))
print "This works!"
np.isfinite(H_estim.values)
print "This does not work!"
这是在以下环境下运行的:
MacOsX Mavericks,Python 2.7.6,numpy 1.8.1,pandas 0.13.1
2 个回答
你假设“所有的元素都是 np.float64,应该没问题。”但实际上可能并不是这样。你的数据结构有多大?你能查看所有的值,找出一些可疑的地方吗?从这个链接 http://matplotlib.1069221.n5.nabble.com/type-error-with-python-3-2-and-version-1-1-1-of-matplotlib-numpy-error-td38784.html 我们可以看到,这个问题可能出现在 Decimal
数据类型上。你有没有办法创建一个简单的示例,能重现这个问题?这样做应该是可行的,而且当你创建这个示例时,很可能就能找到问题所在。
H_estim.values
是一个 numpy 数组,它的数据类型是 object
(你可以查看 H_estim.values.dtype
来确认)。
In [62]: H_estim.values
Out[62]:
array([[3.4000000000000004, 3.6000000000000005, 2.7999999999999998, 3.0],
[3.9000000000000004, 4.3000000000000007, 2.6999999999999993,
3.0999999999999996]], dtype=object)
In [63]: H_estim.values.dtype
Out[63]: dtype('O')
在一个 object
数组中,存储在数组内存中的数据其实是指向 Python 对象的“指针”,而不是对象本身。这里的对象是 np.float64
的实例:
In [65]: H_estim.values[0,0]
Out[65]: 3.4000000000000004
In [66]: type(H_estim.values[0,0])
Out[66]: numpy.float64
所以在很多方面,这个数组看起来和 np.float64
数组很像,实际上却不完全一样。特别是,numpy 的一些函数(比如 np.isfinite
)并不支持处理对象数组。
H_estim.values.astype(np.float64)
这个操作会把数组转换成数据类型为 np.float64
的数组(也就是说,数组里的元素是真正的浮点数值,而不是指向对象的指针)。你可以把这个结果和上面 H_estim.values
的输出进行比较。
In [70]: a = H_estim.values.astype(np.float64)
In [71]: a
Out[71]:
array([[ 3.4, 3.6, 2.8, 3. ],
[ 3.9, 4.3, 2.7, 3.1]])
In [72]: a.dtype
Out[72]: dtype('float64')