gpt4 book ai didi

python - numpy.asarray : how to check up that its result dtype is numeric?

转载 作者:IT老高 更新时间:2023-10-28 20:39:42 24 4
gpt4 key购买 nike

我必须从具有 int、float 或复数的类数组数据创建一个 numpy.ndarray

我希望用 numpy.asarray 函数来做。

我不想给它一个严格的 dtype 参数,因为我想将复杂值转换为 complex64complex128, float float32float64

但是,如果我只是简单地运行 numpy.ndarray(some_unknown_data) 并查看其结果的 dtype,我怎么能理解数据是数字,而不是对象或字符串或其他东西?

最佳答案

您可以检查数组的 dtype 是否是 np.number 的子 dtype。例如:

>>> np.issubdtype(np.complex128, np.number)
True
>>> np.issubdtype(np.int32, np.number)
True
>>> np.issubdtype(np.str_, np.number)
False
>>> np.issubdtype('O', np.number) # 'O' is object
False

本质上,这只是检查 dtype 是否低于 NumPy dtype hierarchy 中的“数字”。 :

enter image description here

关于python - numpy.asarray : how to check up that its result dtype is numeric?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29518923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com