简体   繁体   中英

Function returned when loading .npy file in python3

I have stored a dict as diversity.npy. the dict structure: {key: array}. when I use np.load to load diversity.npy:

diver=np.load('diversity.npy').item()
print(diver)

if the environment is python27 ,it is correct to load the data. However function object is loaded in python34.

<function diversity at 0x7fc0741977b8>

how to fix?

Here's a simple dictionary save in Py3.5

In [157]: dd = {'x':np.array(10)}
In [158]: np.save('dd.npy', dd)
In [159]: data = np.load('dd.npy')
In [160]: data
Out[160]: array({'x': array(10)}, dtype=object)
In [161]: data.item()
Out[161]: {'x': array(10)}

It wrapped the dictionary in an object array, and saved that (using dictionary pickle). item() takes the dictionary out of the array wrapper.

pickle has problems crossing from py2 to py3 .

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