Home > Mobile >  Numpy IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer
Numpy IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer

Time:10-18

Can someone please explain, why I get this error?

IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

g = {}

for k, v in test.items():
   g[k] = np.array(list(v.values()))

 np.savez(r"path", wholedict = g)

 x = np.load(r"path",allow_pickle=True)

 
 print(x['wholedict']['m11'])

Test Data:

test = {'m11': {0: 201.90605394025496, 1: 201.7676375756387, 2: 201.75517262726447, 3: 
201.7900147528744, 4: 201.73350261717178, 5: 201.7676186876304, 6: 201.81307278914971}, 
'm1': {0: 0.01861676364321941, 1: 0.015388273503009661, 2: 0.0335027151600336, 3: 
0.027337348208379984, 4: 0.011926068874735763, 5: 0.016561377848640917, 6: 
0.02375227602659384}, 
'm3': {0: 201.90207282667973, 1: 201.76536469303875, 2: 201.73804333041127, 3: 
201.77188508596834, 4: 201.7346134962993, 5: 201.75237638212087, 6: 201.8108527063905}, 
'm4': {0: 0.024853420811447043, 1: 0.010229421797780105, 2: 0.024384354491196338, 3: 
0.033811524418608886, 4: 0.018298754362813904, 5: 0.01574761594354643, 6: 
0.0183965188129852}, 
'm8': {0: 201.92642049681, 1: 201.76511352507583, 2: 201.74215154608657, 3: 
201.78784338172434, 4: 201.7209310795701, 5: 201.75565717818736, 6: 201.80659280082318}, 
'm9': {0: 0.01993625570264652, 1: 0.011185064011909919, 2: 0.03519709422188439, 3: 
0.03335111143109087, 4: 0.010961924205809446, 5: 0.02122566942052034, 6: 
0.02094117887034937}, 
'm10': {0: -133.47123570009052, 1: -124.85016005718725, 2: -117.22020617423416, 3: 
-108.65787964001079, 4: -100.1153608168557, 5: -91.91280344474521, 6: 
-83.15205199051783}}

CodePudding user response:

Making a dict and saving it as you do:

In [2]: g = {'foo':np.arange(3), 'bar':np.ones((2,10))}
In [3]: np.savez('test1',adict=g)

loading - note the allow_pickle - that's an important clue:

In [8]: dd = np.load('test1.npz', allow_pickle=True)

list(dd.keys()) shows only one file, 'adict':

In [9]: dd['adict']
Out[9]: 
array({'foo': array([0, 1, 2]), 'bar': array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])}, dtype=object)

Read that display carefully - that's an object dtype array with 1 element. That element is a dict. We can extract that element with item:

In [11]: dd['adict'].item()
Out[11]: 
{'foo': array([0, 1, 2]),
 'bar': array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])}

And then use 'foo':

In [12]: dd['adict'].item()['foo']
Out[12]: array([0, 1, 2])

np.save saves arrays. If the argument is not an array, is first "wrapped" with np.array(arg). For a dict, the result is this kind of 1 element object dtype array.

correct save

The correct way to use a dict with savez is to expand it with **:

In [13]: np.savez('test1',**g)
In [14]: dd = np.load('test1.npz', allow_pickle=True)
In [15]: list(dd.keys())
Out[15]: ['foo', 'bar']
In [16]: dd['foo']
Out[16]: array([0, 1, 2])
  • Related