File operations on ndarrays
Most NumPy arrays are read in from files and, after processing, written out back to files.
File operations with text files
The key advantages of text files are that they are human-readable and compatible with any custom software.
Let's start with the following random array:
arr
This array contains the following data:
array([[-0.50566069, -0.52115534,  0.0757591 ],        [ 1.67500165, -0.99280199,  0.80878346],        [ 0.56937775,  0.36614928, -0.02532004]])
The numpy.savetxt(...)
method saves the ndarray to disk in text format.
The following example uses a fmt='%0.2lf'
format string and specifies a comma delimiter:
np.savetxt('arr.csv', arr, fmt='%0.2lf', delimiter=',')
Let's inspect the arr.csv
file written out to disk in the current directory, as follows:
!cat arr.csv...