问题描述
- python数据转换的一个问题,关于isfinite( )函数的输入参数 20C
- Eclipse 上运行的python2.7 控制台,从数据库里读出数据并调用函数处理
cursor.execute(sqlorder)val=np.array(cursor.fetchall())hw=np.size(val axis=0)np.size(val axis=1)#val1=np.zeros((hw)) for i in np.arange(0h): for j in np.arange(0w): #val[i][j]=np.float64(val[i][j]) val[i][j]=np.float64(val[i][j]) isfinite(val)
val是从数据库里读出的矩阵,元素类型是Decimal,如下
[[Decimal('0.732905') Decimal('0.584379') Decimal('0.522045') ...
Decimal('0.359959') Decimal('182.000000') Decimal('39.195782')]
[Decimal('0.721627') Decimal('0.595321') Decimal('0.515621') ...
Decimal('0.353361') Decimal('202.000000') Decimal('50.023821')]
[Decimal('0.744268') Decimal('0.592377') Decimal('0.430369') ...
Decimal('0.346210') Decimal('283.000000') Decimal('58.231160')]
...
[Decimal('0.756491') Decimal('0.571471') Decimal('0.514521') ...
Decimal('0.358508') Decimal('182.000000') Decimal('43.042124')]
[Decimal('0.773865') Decimal('0.577030') Decimal('0.514938') ...
Decimal('0.357403') Decimal('182.000000') Decimal('42.431174')]
[Decimal('0.760719') Decimal('0.573509') Decimal('0.509935') ...
Decimal('0.356843') Decimal('182.000000') Decimal('39.920126')]]
,
强制转换为float64之后调用isfinite()来判断是否有无穷大数,
转换后val=
[[0.73290500000000003 0.58437899999999998 0.52204499999999998 ...
0.35995899999999997 182.0 39.195782000000001]
[0.72162700000000002 0.59532099999999999 0.515621 ... 0.35336099999999998
202.0 50.023820999999998]
[0.74426800000000004 0.59237700000000004 0.430369 ... 0.34621000000000002
283.0 58.231160000000003]
...
[0.75649100000000002 0.57147099999999995 0.51452100000000001 ...
0.35850799999999999 182.0 43.042124000000001]
[0.77386500000000003 0.57703000000000004 0.51493800000000001 ...
0.35740300000000003 182.0 42.431173999999999]
[0.76071900000000003 0.57350900000000005 0.50993500000000003 ...
0.35684300000000002 182.0 39.920126000000003]]
在当前位置上转换后,调用isfinite()出现如下错误:
TypeError: 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''
显示不支持该数据类型重新开辟空间转换,
cursor.execute(sqlorder)val=np.array(cursor.fetchall())hw=np.size(val axis=0)np.size(val axis=1)val1=np.zeros((hw)) for i in np.arange(0h): for j in np.arange(0w): val1[i][j]=np.float64(val[i][j]) #val[i][j]=np.float64(val[i][j]) isfinite(val1)
结果如下:val1=
[[ 0.732905 0.584379 0.522045 ... 0.359959 182. 39.195782]
[ 0.721627 0.595321 0.515621 ... 0.353361 202. 50.023821]
[ 0.744268 0.592377 0.430369 ... 0.34621 283. 58.23116 ]
...
[ 0.756491 0.571471 0.514521 ... 0.358508 182. 43.042124]
[ 0.773865 0.57703 0.514938 ... 0.357403 182. 42.431174]
[ 0.760719 0.573509 0.509935 ... 0.356843 182. 39.920126]]val1调用isfinite可以成功
val 和val1的类型以及子元素的类型如下
除了转换精度和位置实在看不出区别,怀疑有底层机制制约,但是没有查到相应资料