【问题标题】:How to use isinstance() on a numpy structured array custom type如何在 numpy 结构化数组自定义类型上使用 isinstance()
【发布时间】:2020-09-22 23:55:09
【问题描述】:

在下面的示例中,我创建了一个自定义类型,然后创建了一个此类型元素的数组,然后我使用 isinstance() 针对此类型测试此数组的第一个元素,但我得到了一个错误。

import numpy as np

# Here I define a simple type with two fields
my_type_simple = np.dtype([('field_1', int), ('field_2', float)])
# An array using the above type
my_var_simple_1 = np.array([(1, 1), (2, 2)], dtype=my_type_simple)
# For a check, should print [(1, 1.) (2, 2.)]
print(my_var_simple_1)
# For a check, should print True
print(isinstance(my_var_simple_1, np.ndarray))
# The below prints numpy.void - how can I find out that in fact it is 'my_type_simple' ?
print(type(my_var_simple_1[0]))
# The below prints True, at least
print(isinstance(my_var_simple_1[0], type(my_var_simple_1[0])))
# But the below raises an Error: TypeError: isinstance() arg 2 must be a type or tuple of types
print(isinstance(my_var_simple_1[0], my_type_simple))

因此问题是:我如何测试才能发现my_var_simple_1[0]type 实际上是my_simple_type?这可能吗?

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    尝试一下,看看是否适合您的需求:

    my_var_simple_1[0].dtype == my_type_simple
    

    【讨论】:

    • 该死,这很容易,我为什么要修复isinstance()?这里唯一需要注意的是,如果我处理一个列表,它不会有dtype 字段,但这很容易解决。感谢您的帮助!
    • @Maciek。你会在一个类似于 numpy dtype 实例的列表中放入什么?
    • @MadPhysicist 抱歉,我不明白这个问题。
    • @Maciek。你说“这里唯一需要注意的是,如果我处理......”。这意味着什么?这有什么关系?您如何在列表中构建 numpy dtypes?整个问题似乎非常特定于 numpy 数组
    • @MadPhysicist,好吧,只是 numpy 方法往往适用于 ndarray,但也适用于可迭代对象,例如列表,因此我希望我的方法也是通用的。
    【解决方案2】:
    In [7]: my_type_simple = np.dtype([('field_1', int), ('field_2', float)])
       ...: # An array using the above type
       ...: my_var_simple_1 = np.array([(1, 1), (2, 2)], dtype=my_type_simple)
    

    dtype 命令创建一个dtype 对象,即该类的一个实例。它不是 dtype 的子类。

    In [8]: type(my_type_simple)
    Out[8]: numpy.dtype
    

    使用np.array 创建的对象是一个numpy 数组ndarray。不管dtype如何,都是如此。

    In [11]: type(my_var_simple_1)
    Out[11]: numpy.ndarray
    

    对于复合数据类型,元素的类型是void。您的两个字段中的每个字段的 typenp.int64np.float64,但组合是 np.void

    In [12]: type(my_var_simple_1[0])
    Out[12]: numpy.void
    

    但我们可以访问数组或其元素的dtype,并测试:

    In [13]: my_var_simple_1.dtype
    Out[13]: dtype([('field_1', '<i8'), ('field_2', '<f8')])
    In [16]: my_var_simple_1[0].dtype
    Out[16]: dtype([('field_1', '<i8'), ('field_2', '<f8')])
    

    虽然typeisinstance 可用于检查对象是否为ndarray 而不是list 或其他对象,但dtype 在检查数组本身的属性时更有用。 (要求 alist.dtype 会引发错误,因为列表没有这样的属性。)(对象 dtype 数组更像列表。)

    【讨论】:

    • 谢谢,事实上我意识到了我想的所有这些。结构化类型是 np.void 的事实不允许我测试各种类型。我可以打印到屏幕dtype([('field_1', '&lt;i8'), ('field_2', '&lt;f8')]),但不知何故,我固定在使用isinstance(),而不是按照他接受答案中建议的方式使用== 运算符。
    【解决方案3】:

    您正在创建自定义numpy dtypeisinstance 不适用于 ndarray 的内容。欲了解更多信息,请阅读this。 对于您的问题,您可以这样做:

    type(my_var_simple_1[0].item())
    

    或者像Or Y提到的:

    my_var_simple_1[0].dtype
    

    【讨论】:

    • 是的,这个我已经考虑过了,但是需要专门创建一个数据元素来执行测试。感谢您的链接,我之前在寻找相关讨论时没有找到这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多