如何使用savetxt()和loadtxt()函数加载和保存3D Numpy数组文件

如何使用savetxt()和loadtxt()函数加载和保存3D Numpy数组文件

在Python中使用数组时,通常使用NumPy。有时,数据存储在多维或3D数组中。如果使用loadtxt()或savetxt()函数保存或加载数组数据,将需要一个2D数组。如果使用3D数组,则会出现错误:“ValueError: Expected 1D or 2D array, got 3D array instead”。

因此,在这篇Python和Numpy文章中,使用两个不同的示例,编写代码展示了在使用savetxt()和loadtxt()函数以及处理3D数组时保存和加载数组的过程。在第一个示例中,Google Colab上的Python程序使用savetxt()和loadtxt()函数来处理TXT文件。在另一个示例中,这些函数将用于CSV文件。

示例1:利用savetxt()和loadtxt()函数处理TXT文件

设计步骤和编码

  • 步骤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工作表的代码单元格中编写以下代码

import numpy as npp
from numpy import newaxis
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)

#Changing the array shape to 2D
myarray_reshaped = myarray.reshape(myarray.shape[0], -1)
print("The rehaped 2-d array: ")
print(myarray_reshaped)
#print(myarray_reshaped.base)

# saving this reshaped array to myfile.txt
npp.savetxt("myfile.txt", myarray_reshaped)

# loading the reshaped array data from myfile.txt
loaded_myarray = npp.loadtxt("myfile.txt")
print("loaded_myarray shape: ", loaded_myarray.shape)

#Changing the array shape back to 3D
backtomyarray= loaded_myarray.reshape(myarray.shape[0], myarray.shape[1], myarray.shape[2])
print("backtomyarray shape : ", backtomyarray.shape)

# checking if both the Arrays are same
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笔记本并在其中编写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工作表的代码单元中编写以下代码

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)

#Changing the array shape to 2D
myarray_reshaped = myarray.reshape(myarray.shape[0], myarray.shape[1]*myarray.shape[2])
print("The rehaped 2-d array: ")
print(myarray_reshaped)
# saving this reshaped array to 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)
#Changing the array shape back to 3D
backtomyarray= csvdata.reshape(myarray.shape[0], myarray.shape[1], myarray.shape[2])
print("backtomyarray shape : ", backtomyarray.shape)

# checking if both the Arrays are same
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()函数的方法。首先,给出了使用TXT文件配合这些函数使用savetxt()和loadtxt()函数的方法,第二个示例则使用CSV文件和这些函数。程序代码和语法应该被仔细编写以确保程序的执行。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程