gpt4 book ai didi

python - 为什么一些 numpy 数据类型是 JSON 可序列化的,而另一些则不是?

转载 作者:太空狗 更新时间:2023-10-29 21:02:28 27 4
gpt4 key购买 nike

Numpy有很多不同的基本类型,都是listed here .

我已经在我的程序中追踪到 float32 不是 JSON 序列化的问题,所以我已经开始测试上面列表中的所有数据类型:

>>> import numpy as np
>>> from json import dumps
>>> dumps(np.bool(True))
'true'
>>> dumps(np.bool_(True))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: True is not JSON serializable
>>> dumps(np.int(0))
'0'
>>> dumps(np.int_(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.intc(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.intp(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int8(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int16(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int32(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.int64(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint8(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint16(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint32(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.uint64(0))
Traceback (most recent call last):
[...]
TypeError: 0 is not JSON serializable
>>> dumps(np.float(0))
'0.0'
>>> dumps(np.float_(0))
'0.0'
>>> dumps(np.float16(0))
Traceback (most recent call last):
[...]
TypeError: 0.0 is not JSON serializable
>>> dumps(np.float32(0))
Traceback (most recent call last):
[...]
TypeError: 0.0 is not JSON serializable
>>> dumps(np.float64(0))
'0.0'
>>> dumps(np.complex(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex_(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex64(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable
>>> dumps(np.complex128(0))
Traceback (most recent call last):
[...]
TypeError: 0j is not JSON serializable

所以,没有 complex 类型是可序列化的,这是有道理的。

但是 bool 有效而 bool_ 无效。 int 有效,但名称中带有 int 的任何其他内容均无效。 floatfloat_float64 都可以,但是 float16float32 不行.

为什么会这样? 显然,它们都可以很容易地转换为字符串,堆栈跟踪甚至显示 repr() 被用来显示它们的值。 这可能是无意的?或者这种行为有充分的理由吗?

最佳答案

JSON 可序列化的数据类型都是 Python 内置的:

>>> np.int is int
True
>>> np.float is float
True
>>> np.bool is bool
True

因此,您显示的所有 NumPy 数据类型都不是 JSON 可序列化的。至少是一致的。

np.float_ 与 np.float64 相同(在 MacOS 上测试):

>>> np.float_ is np.float64
True

帮助说:

np.float64

64-bit floating-point number. Character code 'd'. Python float compatible.

鉴于:

np.float32

32-bit floating-point number. Character code 'f'. C float compatible.

因此,Python 浮点兼容类型适用于 json.dumps(),但 C 兼容类型则不行。

关于python - 为什么一些 numpy 数据类型是 JSON 可序列化的,而另一些则不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459168/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com