Storing and retrieving NumPy arrays
Most realistic applications deal with large datasets and will require results to be stored in persistent media, such as a hard disk. NumPy arrays can be stored in either text or binary format and binary files can be optionally compressed. We start the section by showing you a recipe to store files in text format.
How to do it...
Let us proceed with getting our queries about storing and retrieving NumPy arrays resolved.
Storing a NumPy array in text format
To store a single NumPy array to the disk in text format, use the savetxt()
function. In the next example, we generate a large array and save it to the disk in text format using the following code:
x = np.random.rand(200, 300) np.savetxt('array_x.txt', x)
This code first uses the np.random.rand()
function to generate a 200 x 300 array of random floats. Next, the savetxt()
NumPy function creates the array_x.txt
file on the disk, containing a text representation of the x
array. The file can be opened with a text...