英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论