Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to load and save 3D Numpy Array file using savetxt() and loadtxt() functions?
When working with 3D NumPy arrays, savetxt() and loadtxt() functions cannot directly handle them since they expect 2D arrays. To save and load 3D arrays, you need to reshape them to 2D format first, then reshape back to 3D after loading.
The Problem with 3D Arrays
Using savetxt() or loadtxt() with 3D arrays directly throws an error:
ValueError: Expected 1D or 2D array, got 3D array instead
Solution: Reshape Before Saving and After Loading
The solution involves three steps:
- Reshape 3D array to 2D before saving
- Save/load using
savetxt()/loadtxt() - Reshape back to original 3D shape after loading
Example 1: Working with TXT Files
import numpy as np
# Create a 3D array of shape (3, 2, 2)
array_3d = np.array([[[3, 18], [46, 79]],
[[89, 91], [66, 75]],
[[77, 34], [21, 19]]])
print("Original 3D array:")
print(array_3d)
print("Shape:", array_3d.shape)
# Reshape to 2D for saving
array_2d = array_3d.reshape(array_3d.shape[0], -1)
print("\nReshaped 2D array:")
print(array_2d)
# Save to TXT file
np.savetxt("data.txt", array_2d)
# Load from TXT file
loaded_array = np.loadtxt("data.txt")
print("\nLoaded array shape:", loaded_array.shape)
# Reshape back to original 3D shape
restored_3d = loaded_array.reshape(array_3d.shape)
print("Restored 3D array shape:", restored_3d.shape)
# Verify arrays are identical
print("Arrays are identical:", np.array_equal(array_3d, restored_3d))
Original 3D array: [[[ 3 18] [46 79]] [[89 91] [66 75]] [[77 34] [21 19]]] Shape: (3, 2, 2) Reshaped 2D array: [[ 3 18 46 79] [89 91 66 75] [77 34 21 19]] Loaded array shape: (3, 4) Restored 3D array shape: (3, 2, 2) Arrays are identical: True
Example 2: Working with CSV Files
import numpy as np
# Create a 3D array
array_3d = np.array([[[3, 18], [46, 79]],
[[89, 91], [66, 75]],
[[77, 34], [21, 19]]])
print("Original 3D array shape:", array_3d.shape)
# Reshape to 2D
array_2d = array_3d.reshape(array_3d.shape[0], -1)
# Save to CSV file with comma delimiter
np.savetxt("data.csv", array_2d, delimiter=",", fmt="%d")
# Load from CSV file
loaded_csv = np.loadtxt("data.csv", delimiter=",").astype(int)
print("Loaded CSV shape:", loaded_csv.shape)
# Reshape back to 3D
restored_3d = loaded_csv.reshape(array_3d.shape)
print("Restored shape:", restored_3d.shape)
# Verify data integrity
print("Data preserved correctly:", np.array_equal(array_3d, restored_3d))
Original 3D array shape: (3, 2, 2) Loaded CSV shape: (3, 4) Restored shape: (3, 2, 2) Data preserved correctly: True
Key Points
| Aspect | TXT Files | CSV Files |
|---|---|---|
| Delimiter | Whitespace (default) | Comma (specify delimiter=",") |
| Format Control | Default formatting | Use fmt parameter for precision |
| Data Type | Float (default) | Convert with .astype() if needed |
Best Practices
- Always store the original shape before reshaping
- Use
array.reshape(shape[0], -1)to flatten while preserving first dimension - Verify data integrity with
np.array_equal()after loading - Consider using
np.save()/np.load()for better performance with large 3D arrays
Conclusion
To save 3D NumPy arrays using savetxt(), reshape them to 2D first, then reshape back to 3D after loading with loadtxt(). This approach works for both TXT and CSV files while preserving data integrity.
Advertisements
