Python numpy 数据类型的最大允许值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23189506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:26:51  来源:igfitidea点击:

Maximum allowed value for a numpy data type

pythonnumpy

提问by jdmcbr

I am working with numpy arrays of a range of data types (uint8, uint16, int16, etc.). I would like to be able to check whether a number can be represented within the limits of an array for a given datatype. I am imagining something that looks like:

我正在处理一系列数据类型(uint8、uint16、int16 等)的 numpy 数组。我希望能够检查一个数字是否可以在给定数据类型的数组范围内表示。我想象的东西看起来像:

>>> im.dtype
dtype('uint16')
>>> dtype_max(im.dtype)
65535
>>> dtype_min(im.dtype)
0

Does something like this exist? By the way, I feel like this has to have been asked before, but my search came up empty, and all of the "similar questions" appear to be unrelated.

这样的东西存在吗?顺便说一句,我觉得以前应该有人问过这个问题,但我的搜索结果空空如也,所有的“类似问题”似乎都不相关。

Edit: Of course, now that I've asked, one of the "related" questions does have the answer. Oops.

编辑:当然,既然我已经问过了,“相关”问题之一确实有答案。哎呀。

采纳答案by Bruno Gelb

min_value = np.iinfo(im.dtype).min
max_value = np.iinfo(im.dtype).max

docs:

文档:

  • np.iinfo(machine limits for integer types)
  • np.finfo(machine limits for floating point types)
  • np.iinfo(整数类型的机器限制)
  • np.finfo(浮点类型的机器限制)

回答by Alex

You're looking for numpy.iinfofor integer types. Documentation here.

您正在寻找numpy.iinfo整数类型。文档在这里

There's also numpy.finfofor floating point types. Documentation here.

还有numpy.finfo浮点类型。文档在这里