Using Spyder / Python to Open .npy File Using Spyder / Python to Open .npy File numpy numpy

Using Spyder / Python to Open .npy File


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

import numpy as npdata = np.random.normal(0, 1, 100)np.save('data.npy', data)

And read in like

import numpy as npdata = 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 button

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


.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 (here's how: http://docs.scipy.org/doc/numpy/user/install.html)

>>> import numpy as np

Let's 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')

Let's compare the original array vs the one loaded from file (array_loaded)

>>> print 'Loaded:  ', array_loadedLoaded:   [2 3 1 2 2 3 1 2 3 3]>>> print 'Original:', arrayOriginal: [2 3 1 2 2 3 1 2 3 3]