如何使用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 Notebook並在其中編寫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) #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文章中,透過兩個不同的示例,給出瞭如何使用savetxt()和loadtxt()函式處理3D陣列的方法。首先給出了使用savetxt()和loadtxt()函式處理TXT檔案的方法,而第二個示例則使用CSV檔案以及這些函式。程式程式碼和語法應仔細編寫以執行程式。