简体   繁体   中英

What does "[()]" mean when called upon a numpy array?

I just came across this piece of code:

x = np.load(lc_path, allow_pickle=True)[()]

And I've never seen this pattern before: [()] . What does it do and why is this syntacticly correct?


a = np.load(lc_path, allow_pickle=True)
>>> array({'train_ppls': [1158.359413193576, 400.54333992093854, ...],
 'val_ppls': [493.0056070137404, 326.53203520368623, ...],
 'train_losses': [340.40905952453613, 675.6475067138672, ...],
 'val_losses': [217.46258735656738, 438.86770486831665, ...],
 'times': [19.488852977752686, 20.147733449935913, ...]}, dtype=object)

So I guess a is a dict wrapped in an array for some reason by the person who saved it

It a way (the only way) of indexing a 0d array:

In [475]: x=np.array(21)                                                                       
In [476]: x                                                                                    
Out[476]: array(21)
In [477]: x.shape                                                                              
Out[477]: ()
In [478]: x[()]                                                                                
Out[478]: 21

In effect it pulls the element out of the array. item() is another way:

In [479]: x.item()                                                                             
Out[479]: 21
In [480]: x.ndim                                                                               
Out[480]: 0

In

x = np.load(lc_path, allow_pickle=True)[()]

most likely the np.save was given a non-array; and wrapped in a 0d object dtype array to save it. This is a way of recovering that object.

In [481]: np.save('test.npy', {'a':1})                                                         
In [482]: x = np.load('test.npy', allow_pickle=True)                                           
In [483]: x                                                                                    
Out[483]: array({'a': 1}, dtype=object)
In [484]: x.ndim                                                                               
Out[484]: 0
In [485]: x[()]                                                                                
Out[485]: {'a': 1}

In general when we index a nd array, eg x[1,2] we are really doing x[(1,2)] , that is, using a tuple that corresponds to the number of dimensions. If x is 0d, the only tuple that works is an empty one, () .

That's indexing the array with a tuple of 0 indices. For most arrays, this just produces a view of the whole array, but for a 0-dimensional array, it extracts the array's single element as a scalar.

In this case, it looks like someone made the weird choice to dump a non-NumPy object to an array with numpy.save , resulting in NumPy saving a 0-dimensional array of object dtype wrapping the original object. The use of allow_pickle=True and the empty tuple index extracts the object from the 0-dimensional array.

They probably should have picked something other than numpy.save to save this object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM