Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to write a python dictionary with Numpy Nd-Array values into a Json File

Tags:

How do I efficiently write a Python dictionary, where the values are Numpy Nd-Arrays to a Json File? I obtain an error saying that the Numpy Nd-Array is not Json-Serializable. Is there any way to overcome this?

like image 594
AlexGuevara Avatar asked Jun 01 '17 08:06

AlexGuevara


People also ask

How do I convert a NumPy array to JSON?

Use the cls kwarg of the json. dump() and json. dumps() method to call our custom JSON Encoder, which will convert NumPy array into JSON formatted data. To serialize Numpy array into JSON we need to convert it into a list structure using a tolist() function.

Can I save a Python dictionary as JSON?

You can save the Python dictionary into JSON files using a built-in module json. We need to use json. dump() method to do this. Use the indent parameter to prettyPrint your JSON data.

Is NumPy array faster than dictionary?

Also as expected, the Numpy array performed faster than the dictionary. However, the dictionary performed faster in Python than in Julia.

Can a NumPy array be a dictionary key?

You may use the data in numpy array to create a hash which could be used as a key for dictionary.


1 Answers

JSON only supports a limited number of datatypes. If you want to store other types of data as JSON then you need to convert it to something that JSON accepts. The obvious choice for Numpy arrays is to store them as (possibly nested) lists. Fortunately, Numpy arrays have a .tolist method which performs the conversion efficiently.

import numpy as np
import json

a = np.array(range(25), dtype=np.uint8).reshape(5, 5) 
print(a)
print(json.dumps(a.tolist()))

output

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]

.tolist will convert the array elements to native Python types (int or float) if it can do so losslessly. If you use other datatypes I suggest you convert them to something portable before calling .tolist.

like image 164
PM 2Ring Avatar answered Oct 11 '22 12:10

PM 2Ring