Numpyにはさまざまな基本タイプがあり、すべてがlisted hereです。numpyのデータ型がJSONシリアライザブルなのはなぜですか?
私はJSON-、シリアライズされていないsのfloat32
に私のプログラムに問題を突き止めてきたので、私は上記のリストからすべてのデータ型のテストを開始しました:だから
>>> 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
が含まれているものはありません。 float
,float_
およびfloat64
はすべて問題なく、float16
およびfloat32
はありません。
これはなぜですか?明らかに、それらはすべて簡単に文字列に変換できます。スタックトレースは、値を表示するためにrepr()
が使用されていることを示しています。 これは意図しないことでしょうか?それとも、この行動に正当な理由がありますか?
'np.float32(...)'オブジェクトを直接作成していますか?または配列から抽出されますか? 'arr.tolist()'同等のものをシリアル化しようとしましたか? – hpaulj
配列の要素を直列化する必要がある場合は、 '.item()'メソッドを使ってPythonスカラに変換することを検討してください。 – hpaulj
https://stackoverflow.com/questions/27050108/convert-numpy-type-to-python 'numpy型をpythonに変換する ' – hpaulj