如何使用 savetxt() 和 loadtxt() 函数加载和保存 3D Numpy 数组文件?
在 Python 中使用数组时,通常使用 NumPy。有时,数据存储在多维或 3D 数组中。如果使用 loadtxt() 或 savetxt() 函数保存或加载数组数据,则需要 2d 数组。如果使用 3D 数组,则会出现此错误 - "ValueError:预期为 1D 或 2D 数组,但得到的是 3D 数组"。
因此,在这篇 Python 和 Numpy 文章中,使用两个不同的示例,编写代码来展示在使用 savetxt() 和 loadtxt() 函数和处理 3D 数组时保存数组和加载数组的过程。在第一个示例中,Google Colab 上的 Python 程序对 TXT 文件使用了 savetxt() 和 loadtxt() 函数。在另一个示例中,这些函数将用于 CSV 文件。
示例 1:对 TXT 文件使用 savetxt() 和 loadtxt() 函数
设计步骤和编码
步骤 1 − 首先使用 gmail 帐户登录。转到 Google Colab。打开一个新的 Colab Notebook 并在其中编写 Python 代码。
步骤 2 − 使用 numpy 数组,创建形状为 (3,2,2) 的 3D 数组。
步骤 3 − 将此数组的形状更改为 2D。展示数组及其形状。
步骤 4 - 使用 savetxt 函数将重塑后的数组保存到名为 myfile.txt 的 txt 文件中。
步骤 5 - 使用 loadtxt 函数将 myfile.txt 的内容加载到名为 loaded_myarray 的数组中,该数组将具有 2D 数组形状。
步骤 6 - 将此 loaded_myarray 的形状改回 3D。打印新数组并打印其形状。
第 7 步 - 检查此新数组和原始数组的所有元素是否相同。
在 Google Colab Worksheet 的代码单元中写入以下代码
import numpy as npp from numpy import newaxis myarray = npp.array([[[3,18], [46, 79]], [[89, 91], [66, 75]],[[77,34],[21,19]]]) print("三维数组: ",myarray) print("Myarray 形状: ", myarray.shape) #将数组形状更改为二维 myarray_reshaped = myarray.reshape(myarray.shape[0], -1) print("重塑后的二维数组: ") print(myarray_reshaped) #print(myarray_reshaped.base) # 将重塑后的数组保存到 myfile.txt npp.savetxt("myfile.txt", myarray_reshaped) # 从 myfile.txt 加载重塑后的数组数据 loaded_myarray = npp.loadtxt("myfile.txt") print("loaded_myarray shape: ", loaded_myarray.shape) # 将数组形状改回 3D backtomyarray= loaded_myarray.reshape(myarray.shape[0], myarray.shape[1], myarray.shape[2]) print("backtomyarray shape : ", backtomyarray.shape) # 检查两个数组是否相同 if (backtomyarray == myarray).all(): print("All elements are same") else: print("All elements are not same")
输出
The 3-d array: [[[ 3 18] [46 79]] [[89 91] [66 75]] [[77 34] [21 19]]] Myarray shape: (3, 2, 2) The rehaped 2-d array: [[ 3 18 46 79] [89 91 66 75] [77 34 21 19]] loaded_myarray shape: (3, 4) backtomyarray shape : (3, 2, 2) All elements are same
示例 2:分别使用 savetxt 和 loadtxt 函数将 3D 数组(重新整形)保存和加载到 CSV 文件中
设计步骤和编码
步骤 1 - 使用 Google 帐户登录。打开一个新的 Colab Notebook 并在其中编写 python 代码。
步骤 2 - 导入所需的库 numpy。
步骤 3 - 使用 numpy 数组,开发形状为 (3,2,2) 的 3D 数组。打印它并打印其形状。
步骤 4 - 将此数组的形状更改为 2D。打印重塑后的数组并打印其形状。
步骤 5 - 使用 savetxt 函数将重塑后的数组保存到名为 my_array.csv 的 CSV 文件中。
步骤 6 - 利用 loadtxt() 函数将 my_array.csv 的内容加载到 csvdata 中,该 csvdata 将具有 2D 数组形状。
步骤 7 - 将此 csvdata 的形状改回 3D。显示结果数组并打印其形状。
步骤 8 - 验证此新数组和原始数组的所有元素是否相同。
在 Google Colab Worksheet 的代码单元中写入以下代码
import numpy as npp myarray = npp.array([[[3,18], [46, 79]], [[89, 91], [66, 75]],[[77,34],[21,19]]]) print("The 3-d array: ",myarray) print("Myarray shape: ", myarray.shape) #将数组形状更改为二维 myarray_reshaped = myarray.reshape(myarray.shape[0], myarray.shape[1]*myarray.shape[2]) print("重塑后的二维数组: ") print(myarray_reshaped) # 将重塑后的数组保存到 my_array.csv npp.savetxt("my_array.csv", myarray_reshaped, delimiter=",", fmt="%d") mycsv = open("my_array.csv", 'r') print("the mycsv file contains:") print(mycsv.read()) csvdata = npp.loadtxt('my_data.csv', delimiter=',').astype(int) print(csvdata) # 将数组形状改回 3D backtomyarray= csvdata.reshape(myarray.shape[0], myarray.shape[1], myarray.shape[2]) print("backtomyarray shape : ", backtomyarray.shape) # 检查两个数组是否相同 if (backtomyarray == myarray).all(): print("All elements are same") else: print("All elements are not same")
输出
按下代码单元上的播放按钮来查看结果
The 3-d array: [[[ 3 18] [46 79]] [[89 91] [66 75]] [[77 34] [21 19]]] Myarray shape: (3, 2, 2) The rehaped 2-d array: [[ 3 18 46 79] [89 91 66 75] [77 34 21 19]] the mycsv file contains: 3,18,46,79 89,91,66,75 77,34,21,19
结论
在这篇 Python 和 Numpy 文章中,通过两个不同的示例,给出了如何在使用 3D 数组时使用 savetxt() 和 loadtxt() 函数的方法。首先,给出了 savetxt() 和 loadtxt() 函数与 TXT 文件一起使用的方法,而第二个示例中则与这些函数一起使用 CSV 文件。应仔细编写程序代码和语法以执行程序。