Numpy.savetxt() is a method in python in numpy library to save an 1D and 2D array to a file.
Syntax: numpy.savetxt(fname, X, fmt=’%.18e’, delimiter=’ ‘, newline=’\n’, header=”, footer=”, comments=’# ‘, encoding=None)
numpy.loadtxt() is a method in python in numpy library to load data from a text file for faster reading.
Syntax: numpy.loadtxt(fname, dtype=’float’, comments=’#’, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
Saving and loading 3D arrays
As discussed earlier we can only use 1D or 2D array in numpy.savetxt(), and if we use an array of more dimensions it will throw a ValueError – Expected 1D or 2D array, got 3D array instead. Therefore, we need to find a way to save and retrieve, at least for 3D arrays, here’s how you can do this by using some Python tricks.
- Step 1: reshape the 3D array to 2D array.
- Step 2: Insert this array to the file
- Step 3: Load data from the file to display
- Step 4: convert back to the original shaped array
shape of arr: (5, 4, 3) shape of load_original_arr: (5, 4, 3) Yes, both the arrays are same
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- numpy.loadtxt() in Python
- How to save a NumPy array to a text file?
- How to extract paragraph from a website and save it as a text file?
- How to save a Python Dictionary to a CSV File?
- Save a dictionary to a file
- How to Save Pandas Dataframe as gzip/zip File?
- How to Load a Massive File as small chunks in Pandas?
- numpy.load() in Python
- How to create a bar chart and save in pptx using Python?
- Python | OpenCV program to read and save an Image
- How to Filter and save the data as new files in Excel with Python Pandas?
- Python PIL | Image.save() method
- Overriding the save method - Django Models
- Python - How to save canvas in pyqt5?
- Wand save() method in Python
- PyQt5 QSpinBox - How to save its geometry
- Python - Save List to CSV
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.