有一个包含所有类型的NumPy对象吗?

huangapple go评论100阅读模式
英文:

Is there a numpy object containing all types?

问题

I'm writting a unit test of a function that only accepts numpy arrays of type numpy.uint8 and I wanted to test that I get the right exception from all the other types.

So I did a set like this:

self.supported_types = {np.uint8}
self.not_supported_types = {np.bool_,
                                    np.int8,
                                    np.int16,
                                    np.uint16,
                                    np.int32,
                                    np.uint32,
                                    np.int64,
                                    np.uint64,
                                    np.longlong,
                                    np.ulonglong,
                                    np.float16,
                                    np.float32,
                                    np.float64,
                                    np.float128,
                                    np.complex64,
                                    np.complex128,
                                    np.complex256,
                                    np.object_,
                                    np.bytes_,
                                    np.str_,
                                    np.void,
                                    np.datetime64,
                                    np.timedelta64}

I was wondering if there's a way to get a list with all numpy's types without writing all of them as:

all_types = numpy.something() # does this exist?
not_supported = all_types.remove(numpy.uint8)
英文:

I'm writting a unit test of a function that only accepts numpy arrays of type numpy.uint8 and I wanted to test that I get the right exception from all the other types.

So I did a set like this:

self.supported_types = {np.uint8}
self.not_supported_types = {np.bool_,
                                    np.int8,
                                    np.int16,
                                    np.uint16,
                                    np.int32,
                                    np.uint32,
                                    np.int64,
                                    np.uint64,
                                    np.longlong,
                                    np.ulonglong,
                                    np.float16,
                                    np.float32,
                                    np.float64,
                                    np.float128,
                                    np.complex64,
                                    np.complex128,
                                    np.complex256,
                                    np.object_,
                                    np.bytes_,
                                    np.str_,
                                    np.void,
                                    np.datetime64,
                                    np.timedelta64}

I was wondering if there's a way to get a list with all numpy's types without writing all of them as:

all_types = numpy.something() # does this exist?
not_supported = all_types.remove(numpy.uint8)

答案1

得分: 2

你可以尝试使用np.dtype.__subclasses__()

[x.type for x in np.dtype.__subclasses__()]

输出:

[numpy.bool_,
 numpy.int8,
 numpy.uint8,
 numpy.int16,
 numpy.uint16,
 numpy.int32,
 numpy.uint32,
 numpy.int64,
 numpy.uint64,
 numpy.longlong,
 numpy.ulonglong,
 numpy.float32,
 numpy.float64,
 numpy.float128,
 numpy.complex64,
 numpy.complex128,
 numpy.complex256,
 numpy.object_,
 numpy.bytes_,
 numpy.str_,
 numpy.void,
 numpy.datetime64,
 numpy.timedelta64,
 numpy.float16,
 int,
 float,
 complex]

在你的情况下:

supported_types = {np.uint8}
not_supported_types = ({x.type for x in np.dtype.__subclasses__()}
                       - supported_types - {int, complex, float})

输出:

{numpy.bool_,
 numpy.bytes_,
 numpy.complex128,
 numpy.complex256,
 numpy.complex64,
 numpy.datetime64,
 numpy.float128,
 numpy.float16,
 numpy.float32,
 numpy.float64,
 numpy.int16,
 numpy.int32,
 numpy.int64,
 numpy.int8,
 numpy.longlong,
 numpy.object_,
 numpy.str_,
 numpy.timedelta64,
 numpy.uint16,
 numpy.uint32,
 numpy.uint64,
 numpy.ulonglong,
 numpy.void}
英文:

You can maybe use np.dtype.__subclasses__():

[x.type for x in np.dtype.__subclasses__()]

Output:

[numpy.bool_,
 numpy.int8,
 numpy.uint8,
 numpy.int16,
 numpy.uint16,
 numpy.int32,
 numpy.uint32,
 numpy.int64,
 numpy.uint64,
 numpy.longlong,
 numpy.ulonglong,
 numpy.float32,
 numpy.float64,
 numpy.float128,
 numpy.complex64,
 numpy.complex128,
 numpy.complex256,
 numpy.object_,
 numpy.bytes_,
 numpy.str_,
 numpy.void,
 numpy.datetime64,
 numpy.timedelta64,
 numpy.float16,
 int,
 float,
 complex]

In your case:

supported_types = {np.uint8}
not_supported_types = ({x.type for x in np.dtype.__subclasses__()}
                       - supported_types - {int, complex, float})

Output:

{numpy.bool_,
 numpy.bytes_,
 numpy.complex128,
 numpy.complex256,
 numpy.complex64,
 numpy.datetime64,
 numpy.float128,
 numpy.float16,
 numpy.float32,
 numpy.float64,
 numpy.int16,
 numpy.int32,
 numpy.int64,
 numpy.int8,
 numpy.longlong,
 numpy.object_,
 numpy.str_,
 numpy.timedelta64,
 numpy.uint16,
 numpy.uint32,
 numpy.uint64,
 numpy.ulonglong,
 numpy.void}

huangapple
  • 本文由 发表于 2023年3月9日 23:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定