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 editor.

Saving NumPy arrays in text format always implies loss of precision, since the binary data has to be converted to decimal. Text formats also tend to require more disk space. Unless there is need to open the file with a different software package or the file needs to be human-readable, it is recommended that NumPy arrays are saved in binary format. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset