Using Spyder / Python to Open .npy File

Using Spyder / Python to Open .npy File

*.npy files are binary files to store numpy arrays. They
are created with

import numpy as np

data = np.random.normal(0, 1, 100)
np.save(data.npy, data)

And read in like

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

Given that you asked for Spyder, you need to do two things to import those files:

  1. Select the pane called Variable Explorer
  2. Press the import button (shown below), select your .npy file and present Ok.

    import

Then you can work with that file in your current Python or IPython console.

Using Spyder / Python to Open .npy File

.npy files are binary files.
Do not try to open it with Spyder or any text editor; what you see may not make sense to you.

Instead, load the .npy file using the numpy module (reference: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.load.html).

Code sample:

First, import numpy. If you do not have it, install (heres how: http://docs.scipy.org/doc/numpy/user/install.html)

>>> import numpy as np

Lets set a random numpy array as variable array.

>>> array = np.random.randint(1,5,10)
>>> print array
[2 3 1 2 2 3 1 2 3 3]

To export to .npy file, use np.save(FILENAME, OBJECT) where OBJECT = array

>>> np.save(test.npy, array)

You can load the .npy file using np.load(FILENAME)

>>> array_loaded = np.load(test.npy)

Lets compare the original array vs the one loaded from file (array_loaded)

>>> print Loaded:  , array_loaded
Loaded:   [2 3 1 2 2 3 1 2 3 3]

>>> print Original:, array
Original: [2 3 1 2 2 3 1 2 3 3]

Leave a Reply

Your email address will not be published.