使用 Spyder / Python 打开 .npy 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33885051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Spyder / Python to Open .npy File
提问by iron2man
Sorry. I'm just now learning Python and everything there is to do with data analysis.
对不起。我现在正在学习 Python 以及与数据分析有关的一切。
How on earth do I open a .npy file with Spyder? Or do I have to use another program? I'm using a Mac, if that is at all relevant.
我到底如何用 Spyder 打开 .npy 文件?还是我必须使用其他程序?我正在使用 Mac,如果这完全相关的话。
回答by MaxNoe
*.npy
files are binary files to store numpy arrays. They
are created with
*.npy
files 是用于存储 numpy 数组的二进制文件。它们是用
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')
回答by J. Yu
.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.
.npy
文件是二进制文件。不要尝试用 Spyder 或任何文本编辑器打开它;你所看到的可能对你没有意义。
Instead, load the .npy
file using the numpy
module (reference: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.load.html).
相反,.npy
使用numpy
模块加载文件(参考: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)
首先,导入numpy。如果没有,请安装(方法如下:http: //docs.scipy.org/doc/numpy/user/install.html)
>>> import numpy as np
Let's set a random numpy array as variable array
.
让我们将一个随机的 numpy 数组设置为变量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
要导出到.npy
文件,请使用np.save(FILENAME, OBJECT)
whereOBJECT = array
>>> np.save('test.npy', array)
You can load the .npy
file using np.load(FILENAME)
您可以.npy
使用加载文件np.load(FILENAME)
>>> array_loaded = np.load('test.npy')
Let's compare the original array
vs the one loaded from file (array_loaded
)
让我们比较一下原始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]
回答by Carlos Cordoba
Given that you asked for Spyder, you need to do two things to import those files:
鉴于您要求使用 Spyder,您需要做两件事来导入这些文件:
- Select the pane called
Variable Explorer
Press the import button (shown below), select your
.npy
file and presentOk
.
Then you can work with that file in your current Python or IPython console.
然后,您可以在当前的 Python 或 IPython 控制台中使用该文件。
回答by Mona Jalal
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, allow_pickle=True)
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 that is of the format .npy. For example, I have this RGB image
and its depth image is in .npy format. Converting it to png gives me so:
为当前目录中的每个图像创建一个 .npy 格式的 png 文件。例如,我有这个 RGB 图像
,它的深度图像是 .npy 格式。将它转换为 png 给了我这样的: