Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble using numpy.load

I have the following data, written in python 2, that I'd like to load to a python 3 file.

import numpy as np
x = np.array([{'a': np.array([1., 2., 3])}])
np.save('data.npy', x)

My first attempt was this:

import numpy as np
x = np.load('data.npy')

UnicodeError: Unpickling a python object failed

After playing around with the original data I am trying to load, it seems that whenever I have a numpy float inside of a numpy array inside a python dictionary, I get the error. I can load a dictionary, I can load a numpy array, I can even load a numpy array inside a python dictionary, but as soon as I have numpy floats inside a numpy array inside a python dictionary, I get the error. My second attempt was this:

import numpy as np
x = np.load('data.npy', encoding = 'bytes')
x

array([{b'a': array([ 1.,  2.,  3.])}], dtype=object)

This worked in that I could load the data, but it added a 'b' in front of every key in the dictionary. I was wondering if anyone had any insight into why this problem is occuring and how to fix it.

Thanks!

edit:

it seems like the following solved the problem:

import numpy as np
x = np.load('data.npy', encoding = 'latin1')
like image 387
Peter Avatar asked Jul 11 '16 21:07

Peter


People also ask

How can I get the NumPy library to work?

Before you can import numpy, you first need to install it. There are two ways to install numpy: Install the binary (pre-compiled) version using pip. Compile it from source code, and then install it.

What is NumPy load in Python?

load() in Python is used load data from a text file, with aim to be a fast reader for simple text files. Note that each row in the text file must have the same number of values.


1 Answers

The default encoding in Python 2 is ascii; in Python 3 it is utf-8. latin1 (a.k.a., ISO-8859-1) is a superset of ascii. That's why loading ascii-encoded strings with latin1 works and gives the same result as loading it with ascii.

like image 95
A. Garcia-Raboso Avatar answered Oct 13 '22 05:10

A. Garcia-Raboso