Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to load a .npy dictionary using np.load()?

I had a dictionary in this format:

{image_name.jpg:[list_of_features], image_name2.jpg:[list of it's features]}

I saved this dictionary as npy file using this code: np.save('encoded_img_dic.npy', encoded_img_dic) and loaded it back using dic = np.load('/content/encoded_img_dic.npy', allow_pickle = True)

After loading it back, it becomes a np.array and look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array({'1000092795.jpg': array([ 0.18229158, -0.17214337, -0.07549195, ..., -0.01746007,
       -0.10297356,  0.35006437], dtype=float32), '10002456.jpg': array([-0.10733618, -0.08182468, -0.1734893 , ..., -0.13148569,
       -0.12901662, -0.09519334], dtype=float32), '99804383.jpg': array([-0.10597093, -0.1605651 , -0.08017335, ...,  0.03384358,
        0.58321196, -0.06605151], dtype=float32), '998845445.jpg': array([-0.13575825,  0.55340654, -0.01252322, ...,  0.37376422,
        0.02249258,  0.09976979], dtype=float32)}, dtype=object)

This array is a array of dictionary.
It has a dictionary stuck inside it. How do I get that dictionary? so it looks something like this:

{'1000092795.jpg': [ 0.18229158, -0.17214337, -0.07549195, ..., -0.01746007,
           -0.10297356,  0.35006437], '10002456.jpg': [-0.10733618, -0.08182468, -0.1734893 , ..., -0.13148569,
           -0.12901662, -0.09519334], '99804383.jpg': [-0.10597093, -0.1605651 , -0.08017335, ...,  0.03384358,
            0.58321196, -0.06605151], '998845445.jpg':[-0.13575825,  0.55340654, -0.01252322, ...,  0.37376422,
            0.02249258,  0.09976979]}

>Solution :

The np.load gives you a zero-dimensional numpy array. You can get the dictionary out of there by calling the item method.

dic = np.load(filename, allow_pickle=True).item()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading