Python 将3d numpy阵列高速保存到磁盘中
我有一个大小为(1924000)的numpy数组,我想在磁盘上以一种快速的方式写入它。我不在乎格式,我可以在以后转换 我现在写的是以csv格式保存,这需要很长时间:Python 将3d numpy阵列高速保存到磁盘中,python,numpy,multidimensional-array,save,Python,Numpy,Multidimensional Array,Save,我有一个大小为(1924000)的numpy数组,我想在磁盘上以一种快速的方式写入它。我不在乎格式,我可以在以后转换 我现在写的是以csv格式保存,这需要很长时间: for i in range(0,192): np.savetxt(foder+"/{}_{}.csv".format(filename,i), data[i] , "%i", delimiter=", ") 这需要20-25秒。我尝试了在stackoverflow问题和numpy save中找到的pandas D
for i in range(0,192):
np.savetxt(foder+"/{}_{}.csv".format(filename,i), data[i] , "%i", delimiter=", ")
这需要20-25秒。我尝试了在stackoverflow问题和numpy save中找到的pandas DataFrame和Panel方法。它们似乎都运行正常,但当我打开它时,文件夹是空的
你知道如何提高速度吗
为什么代码运行时没有错误,但没有保存任何内容,例如numpy.save 通常保存大型数组的最快方法是将其保存为二进制文件,这可以通过numpy的save命令完成。例如,以下操作将创建一个填充有零的三维数组,将该数组写入文件,然后检索该文件:
a = numpy.zeros((192,192,4000))
numpy.save("mydata.npy",a)
b = numpy.load("mydata.npy")
当然,文件“mydata.npy”应该在save命令之后的当前目录中 在保存之前,您还可以将阵列从
3D重塑为2D
。有关示例,请参见以下代码
import numpy as gfg
arr = gfg.random.rand(5, 4, 3)
# reshaping the array from 3D
# matrice to 2D matrice.
arr_reshaped = arr.reshape(arr.shape[0], -1)
# saving reshaped array to file.
gfg.savetxt("geekfile.txt", arr_reshaped)
# retrieving data from file.
loaded_arr = gfg.loadtxt("geekfile.txt")
# This loadedArr is a 2D array, therefore
# we need to convert it to the original
# array shape.reshaping to get original
# matrice with original shape.
load_original_arr = loaded_arr.reshape(
loaded_arr.shape[0], loaded_arr.shape[1] // arr.shape[2], arr.shape[2])
# check the shapes:
print("shape of arr: ", arr.shape)
print("shape of load_original_arr: ", load_original_arr.shape)
# check if both arrays are same or not:
if (load_original_arr == arr).all():
print("Yes, both the arrays are same")
else:
print("No, both the arrays are not same")
这里有两个不同的问题。第一个是关于将NumPy阵列保存到磁盘的最佳方法,这很简单。第二,为什么代码运行时没有错误,但没有保存任何内容,例如numy.save?!,信息太少不可能回答。保存时我没有在.npy中指定,我现在可以工作了。谢谢保存二维阵列比保存三维阵列快吗?