InputBlackBoxOutput
InputBlackBoxOutput

Reputation: 353

Numpy: Loading data from JSON file gives np array with nested lists

I am loading data from a JSON file as list and then making a NumPy array.

The JSON file is structured as follows

{
    "label": "4",
    "mfcc": [
        [
            [
                -147.2358550730904,
                52.60503152410914,
                <more values Total=13>

            ],
            <more arrays Total=44>

The code I am using to make a NumPy array using data collected from the JSON file

with open("data.json", 'r') as file:
   data = json.load(file)
   
   mfcc = np.array(data["mfcc"])

It seems that the most outer list gets converted to a NumPy array while the inner lists are still lists. See the image below:

Image showing datatypes

What has happened?

Thanks in advance!

Upvotes: 2

Views: 944

Answers (1)

Valentin Vignal
Valentin Vignal

Reputation: 8240

So it means the file contains a numpy array with lists inside.

  • Either is was made on purpose
  • Either you (or someone) tried to convert a list of list into a numpy array but the nested lists are not all with the same length (which is required for a numpy array). Therefore, numpy doesn't create a proper 2D numpy array but a 1D numpy array with lists inside. To fix it, make sure all the nested lists are the same length (you can pad them with 0 for instance)

Upvotes: 1

Related Questions