保存numpy的矩阵 [英] Save a numpy matrix
问题描述
我有一个三维数组,我想获得沿X-Y 2D图像以Z的每一点的最大值,并保存为一个numpy的数组。
I have a 3D array and I would like to obtain a 2D image along X-Y with the maximum value of z at each point and save it as a numpy array.
import numpy as num
matrix=num.load('3d')
nx,ny,nz=num.shape(matrix)
CXY=num.zeros([ny, nx])
for i in range(ny):
for j in range(nx):
CXY[i,j]=num.max(matrix[j,i,:])
的问题是保存所得到的矩阵。我想它numpy.save保存,但我总是得到一个空数组。有没有人有任何建议妥善保存所得到的阵列?
The problem is to save the obtained matrix. I would like to save it with numpy.save but I always get an empty array. Does anyone have suggestions to properly save the obtained array?
我只是用num.save:
I just used num.save:
num.save(最大,CXY [I,J])
num.save('max', CXY[i,j])
推荐答案
我猜你正在寻找它保存在一个可读的格式,而不是其保存为二进制格式numpy.save的numpy.savetxt。
I guess that you're looking for the numpy.savetxt which saves in a human readable format instead of the numpy.save which saves as a binary format.
import numpy as np
matrix=np.random.random((10,10,42))
nx,ny,nz=np.shape(matrix)
CXY=np.zeros([ny, nx])
for i in range(ny):
for j in range(nx):
CXY[i,j]=np.max(matrix[j,i,:])
#Binary data
np.save('maximums.npy', CXY)
#Human readable data
np.savetxt('maximums.txt', CXY)
这code第一次作为一个二进制文件,然后你可以在一个普通的文本编辑器打开一个文件保存阵列。
This code saves the array first as a binary file and then as a file you can open in a regular text editor.
这篇关于保存numpy的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!