如何使用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笔记本并在其中编写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)

#将数组形状更改为2D
myarray_reshaped = myarray.reshape(myarray.shape[0], -1)
print("The rehaped 2-d array: ")
print(myarray_reshaped)
#print(myarray_reshaped.base)

#使用savetxt函数保存此重塑后的数组
npp.savetxt("myfile.txt", myarray_reshaped)

#使用loadtxt函数从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")
Bash

输出

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
Bash

第二个示例:使用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中,它将具有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)

#将数组形状改为2D
myarray_reshaped = myarray.reshape(myarray.shape[0], myarray.shape[1]*myarray.shape[2])
print("The rehaped 2-d array: ")
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")
Bash

输出

按下代码单元格上的 play 按钮查看结果

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
Bash

结论

在这个Python和Numpy的文章中,通过两个不同的例子,展示了如何在使用3D数组时利用savetxt()和loadtxt()函数。第一个示例中使用TXT文件,第二个示例中使用这些函数的同时使用CSV文件。应仔细编写程序代码和语法才能执行程序。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册