Using Spyder / Python to open a .npy file

Unfortunately. I am just learning Python and everything related to data analysis.

How can I open a .npy file with Spyder? Or do I need to use another program? I use a Mac if that matters.

+8
source share
4 answers

*.npyfiles are binary files for storing numpy arrays. They are created using

import numpy as np

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

And read how

import numpy as np
data = np.load('data.npy')
+13
source
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
import glob


for filename in glob.glob("*.*"):
    if '.npy' in filename:
        img_array = np.load(filename)
        plt.imshow(img_array, cmap="gray")
        img_name = filename+".png"
        matplotlib.image.imsave(img_name, img_array)
        print(filename)

creates a png file for each image in the current directory, which has the format .npy. For example, I have this RGB image enter image description here and its depth image is in .npy format. Converting it to png gives me this: enter image description here

+3

.npy . Spyder ; , , .

.npy numpy (: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.load.html).

:

numpy. , ( : http://docs.scipy.org/doc/numpy/user/install.html)

>>> import numpy as np

numpy array.

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

.npy, np.save(FILENAME, OBJECT), OBJECT = array

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

.npy, np.load(FILENAME)

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

array , (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]
+2

, Spyder, :

  • Variable Explorer
  • Click the import button (shown below), select the file .npyand enter Ok.

    import button

You can then work with this file in the current Python or IPython console.

+2
source

Source: https://habr.com/ru/post/1617163/


All Articles