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

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

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:

  1. self.supported_types = {np.uint8}
  2. self.not_supported_types = {np.bool_,
  3. np.int8,
  4. np.int16,
  5. np.uint16,
  6. np.int32,
  7. np.uint32,
  8. np.int64,
  9. np.uint64,
  10. np.longlong,
  11. np.ulonglong,
  12. np.float16,
  13. np.float32,
  14. np.float64,
  15. np.float128,
  16. np.complex64,
  17. np.complex128,
  18. np.complex256,
  19. np.object_,
  20. np.bytes_,
  21. np.str_,
  22. np.void,
  23. np.datetime64,
  24. 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:

  1. all_types = numpy.something() # does this exist?
  2. 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:

  1. self.supported_types = {np.uint8}
  2. self.not_supported_types = {np.bool_,
  3. np.int8,
  4. np.int16,
  5. np.uint16,
  6. np.int32,
  7. np.uint32,
  8. np.int64,
  9. np.uint64,
  10. np.longlong,
  11. np.ulonglong,
  12. np.float16,
  13. np.float32,
  14. np.float64,
  15. np.float128,
  16. np.complex64,
  17. np.complex128,
  18. np.complex256,
  19. np.object_,
  20. np.bytes_,
  21. np.str_,
  22. np.void,
  23. np.datetime64,
  24. 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:

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

答案1

得分: 2

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

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

输出:

  1. [numpy.bool_,
  2. numpy.int8,
  3. numpy.uint8,
  4. numpy.int16,
  5. numpy.uint16,
  6. numpy.int32,
  7. numpy.uint32,
  8. numpy.int64,
  9. numpy.uint64,
  10. numpy.longlong,
  11. numpy.ulonglong,
  12. numpy.float32,
  13. numpy.float64,
  14. numpy.float128,
  15. numpy.complex64,
  16. numpy.complex128,
  17. numpy.complex256,
  18. numpy.object_,
  19. numpy.bytes_,
  20. numpy.str_,
  21. numpy.void,
  22. numpy.datetime64,
  23. numpy.timedelta64,
  24. numpy.float16,
  25. int,
  26. float,
  27. complex]

在你的情况下:

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

输出:

  1. {numpy.bool_,
  2. numpy.bytes_,
  3. numpy.complex128,
  4. numpy.complex256,
  5. numpy.complex64,
  6. numpy.datetime64,
  7. numpy.float128,
  8. numpy.float16,
  9. numpy.float32,
  10. numpy.float64,
  11. numpy.int16,
  12. numpy.int32,
  13. numpy.int64,
  14. numpy.int8,
  15. numpy.longlong,
  16. numpy.object_,
  17. numpy.str_,
  18. numpy.timedelta64,
  19. numpy.uint16,
  20. numpy.uint32,
  21. numpy.uint64,
  22. numpy.ulonglong,
  23. numpy.void}
英文:

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

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

Output:

  1. [numpy.bool_,
  2. numpy.int8,
  3. numpy.uint8,
  4. numpy.int16,
  5. numpy.uint16,
  6. numpy.int32,
  7. numpy.uint32,
  8. numpy.int64,
  9. numpy.uint64,
  10. numpy.longlong,
  11. numpy.ulonglong,
  12. numpy.float32,
  13. numpy.float64,
  14. numpy.float128,
  15. numpy.complex64,
  16. numpy.complex128,
  17. numpy.complex256,
  18. numpy.object_,
  19. numpy.bytes_,
  20. numpy.str_,
  21. numpy.void,
  22. numpy.datetime64,
  23. numpy.timedelta64,
  24. numpy.float16,
  25. int,
  26. float,
  27. complex]

In your case:

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

Output:

  1. {numpy.bool_,
  2. numpy.bytes_,
  3. numpy.complex128,
  4. numpy.complex256,
  5. numpy.complex64,
  6. numpy.datetime64,
  7. numpy.float128,
  8. numpy.float16,
  9. numpy.float32,
  10. numpy.float64,
  11. numpy.int16,
  12. numpy.int32,
  13. numpy.int64,
  14. numpy.int8,
  15. numpy.longlong,
  16. numpy.object_,
  17. numpy.str_,
  18. numpy.timedelta64,
  19. numpy.uint16,
  20. numpy.uint32,
  21. numpy.uint64,
  22. numpy.ulonglong,
  23. 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:

确定