Numpy:检查整数 NaN
Numpy : check for integer NaN
我需要一个 table 包含不同数据类型(浮点数或整数)的列。
我使用 dtype 来定义它们:
import numpy as np
# define array
datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<i4') ]
arr = np.full((4,), np.nan, dtype=datadef)
# fill array with data
arr['i'] = np.array([1, 2, 3, 4])
arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])
arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])
# nothing for 'j'
print arr
输出:
[(1, 1.33333333, 2.77777778, -2147483648)
(2, nan, 5.4 , -2147483648)
(3, 2.66666667, 3.4 , -2147483648)
(4, 5. , nan, -2147483648)]
最后一列的 NaN
值已转换为 -2147483648
,目前没有问题。
但现在我无法检查数组中的值是否确实为 NaN :
row = arr[1]
print np.isnan(row) # TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
并且在单个单元格上似乎 NaN
信息丢失并且 -2147483648
被视为 "classic number" :
print row # (2, nan, 5.4, -2147483648)
print np.isnan(row[0]) # False, OK
print np.isnan(row[1]) # True, OK
print np.isnan(row[3]) # False, expected True
在这种情况下,是否有一种简单的方法来检查 NaN
整数?
np.nan
是浮点数而不是整数。您要么必须更改最后一列的数据类型,要么使用不同的结构将您的 nan 存储为整数。
datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<f4') ]
arr = np.full((4,), np.nan, dtype=datadef)
# fill array with data
arr['i'] = np.array([1, 2, 3, 4])
arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])
arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])
# nothing for 'j'
现在尝试打印 np.isnan 语句:
print(np.isnan(arr[1][3]))
True
我需要一个 table 包含不同数据类型(浮点数或整数)的列。
我使用 dtype 来定义它们:
import numpy as np
# define array
datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<i4') ]
arr = np.full((4,), np.nan, dtype=datadef)
# fill array with data
arr['i'] = np.array([1, 2, 3, 4])
arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])
arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])
# nothing for 'j'
print arr
输出:
[(1, 1.33333333, 2.77777778, -2147483648)
(2, nan, 5.4 , -2147483648)
(3, 2.66666667, 3.4 , -2147483648)
(4, 5. , nan, -2147483648)]
最后一列的 NaN
值已转换为 -2147483648
,目前没有问题。
但现在我无法检查数组中的值是否确实为 NaN :
row = arr[1]
print np.isnan(row) # TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
并且在单个单元格上似乎 NaN
信息丢失并且 -2147483648
被视为 "classic number" :
print row # (2, nan, 5.4, -2147483648)
print np.isnan(row[0]) # False, OK
print np.isnan(row[1]) # True, OK
print np.isnan(row[3]) # False, expected True
在这种情况下,是否有一种简单的方法来检查 NaN
整数?
np.nan
是浮点数而不是整数。您要么必须更改最后一列的数据类型,要么使用不同的结构将您的 nan 存储为整数。
datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<f4') ]
arr = np.full((4,), np.nan, dtype=datadef)
# fill array with data
arr['i'] = np.array([1, 2, 3, 4])
arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])
arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])
# nothing for 'j'
现在尝试打印 np.isnan 语句:
print(np.isnan(arr[1][3]))
True