2017-06-09 51 views
6

Numpy, hepsi listed here olan çok farklı temel türlere sahiptir.Neden bazı numpy veri tabanları JSON serileştirilebilir ve diğerleri değil?

JSON serileştirilebilir olmama s float32 benim programda bir sorunu izini, o yüzden yukarıdaki listeden tüm veri tiplerini test başladım: Yani

>>> 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 

, hiçbir complex tip seri hale getirilebilir , bu mantıklı.

bool ve bool_ çalışmıyor. int çalışır, ancak adında int ile başka bir şey yok. float, float_ ve float64 tüm iyi, ancak float16 ve float32 değil.

Bu neden böyle? Açıkçası, hepsi kolayca bir dizeye dönüştürülebilir, stacktrace bile değerlerini göstermek için kullanılan repr() gösterir. Bu durumun kasıtsız olabilmesi mümkün mü? Yoksa bu davranış için iyi bir neden var mı? serializable JSON olan

+0

Doğrudan 'np.float32 (...)' nesnesini mi oluşturuyorsunuz? Yoksa bir diziden mi çıkarıldı? 'Arr.tolist() 'eşdeğerini serileştirmeyi denediniz mi? – hpaulj

+0

Dizinin bir elemanını serileştirmeniz gerekiyorsa, bir Python skalar dosyasına dönüştürmek için '.item()' yöntemini kullanmayı düşünün. – hpaulj

+0

Ayrıca bkz. Https://stackoverflow.com/questions/27050108/convert-numpy-type-to-python 'Numune türünü python'a dönüştür ' – hpaulj

cevap

4

veri tipleri bütün Python yerleşik ins şunlardır:

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

Yani göstermek tüm NumPy veri türleri JSON seri hale getirilebilir değildir. En azından tutarlı.

np.float _

(MacOS üzerinde test) np.float64 aynıdır:

>>> np.float_ is np.float64 
True 

yardım diyor ki:

np.float64

64-bit kayan noktalı sayı. Karakter kodu 'd'. Python uyumludur.

ise:

np.float32

32-bit kayan nokta sayısı. Karakter kodu 'f'. C şamandıra uyumlu.

Bu nedenle, Python float uyumlu türleri json.dumps() ile çalışır, ancak C uyumlu olanlar çalışmaz.

+0

O zaman neden float64' ve float_' çalışıyor? '>>> np.float64 float' ve' >> np.float_ float' her ikisi de 'false' döndürür. – iFreilicht

+0

Güncellenmiş cevabımı görün. –

+1

Ayrıca '__mro__' bakabilirsiniz. np.float64 .__ mro__', float, np.float32 .__ mro__' içermiyor. – hpaulj

İlgili konular