Cat*_*aCM 2 python arrays numpy
我想保存一个形状为(5,2)的数组,该数组名为sorted_cube_station_list.
在打印它看起来没问题,但当我用numpy.tofile保存它,然后用numpy.fromfile读取它,它就像一个1d数组
你能帮帮我吗?将numpy导入为num
nx=5
ny=5
nz=5
stations=['L001','L002','L003','L004','L005']
for x in range(nx):
for y in range (ny):
for z in range (nz):
cube_station_list = []
i=-1
for sta in stations:
i=i+1
cube=[int(i), num.random.randint(2500, size=1)[0]]
cube_station_list.append(cube)
cub_station_list_arr=num.asarray(cube_station_list)
sorted_cube_station_list_arr=cub_station_list_arr[cub_station_list_arr[:, 1].argsort()]
print x,y,z, sorted_cube_station_list_arr
num.ndarray.tofile(sorted_cube_station_list_arr,str(x)+'_'+str(y)+'_'+str(z)
Run Code Online (Sandbox Code Playgroud)
我建议你用 np.save
a = np.ones(16).reshape([8, 2])
np.save("fileName.npy", a)
Run Code Online (Sandbox Code Playgroud)
请参阅docs:first参数不能是要保存的变量,而是要保存的文件的路径.因此,使用时出现的错误np.save(yourArray)
您可以使用加载已保存的数组 np.load(pathToArray)