Friday, March 4, 2016

QuickTip: Save NumPy arrays to File as Column Vectors

Consider:

import numpy as np

x=np.array([1, 2, 3])
y=x*x

print np.c_[x,y]
array([[1, 1],
       [2, 4],
       [3, 9]])

So if you want to save two arrays (of the same size) to file then,

np.savetxt('xy.dat', np.c_[x,y], fmt='%3.4f')

No comments: