代码:
import sys
import numpy as np
print(f"We are using Python {sys.version}", file=sys.stderr)
print(f"We are using numpy version {np.__version__}", file=sys.stderr) # 2.2.1
def find_non_numpy_floats(x: any) -> bool:
if not (isinstance(x, np.float64)):
print(f"Found non-numpy.float64: {x} of type {type(x)}", file=sys.stderr)
return False
else:
return True
w: np.ndarray = np.zeros((2, 2), dtype=np.float64)
np.vectorize(lambda x: find_non_numpy_floats(x))(w)
assert (np.all(np.vectorize(lambda x: isinstance(x, np.float64))(w))), "try to keep using the numpy floats"
我期望Numpy.zeros生成一个 Numpy 数组float64
,如果我理解正确的话,它与 Python 不同float
(IEEE 64 位浮点数与 Python 特有的某些东西?)
然而,上述结果是:
We are using Python 3.13.1 (main, Dec 9 2024, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]
We are using numpy version 2.2.1
Found non-numpy.float64: 0.0 of type <class 'float'>
Found non-numpy.float64: 0.0 of type <class 'float'>
Found non-numpy.float64: 0.0 of type <class 'float'>
Found non-numpy.float64: 0.0 of type <class 'float'>
以及一个断言错误。
为什么会这样?我该如何解决这个问题(我应该这样做吗?)