I assume I'm asking a newbie question but have spent too much time today searching for an answer. I get an IndexError: too many indices for array error when naively attempting to perform the same slice operation on a numpy array after saving and reloading with np.genfromtxt.
Note: I see that the dimension has changed from (3,6) to (3,) upon reloading but was unable to convert the result back to dimensions (3,6)- this is the part I assume must be obvious (or maybe I need to specify type differently)
yo = np.arange(18)
yo = yo.reshape(3,6)
print(yo)
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]]
print(yo[:,:2])
[[ 0 1]
[ 6 7]
[12 13]]
np.savetxt("test_data.csv", yo, delimiter=",", fmt='%1.4e')
yo_reloaded = np.genfromtxt("test_data.csv", dtype=(float, float, float, float, float, float), delimiter = ",")
#same as above but doesn't work
print(yo_reloaded[:,:2])
IndexError: too many indices for array
print(yo_reloaded)
[( 0., 1., 2., 3., 4., 5.) ( 6., 7., 8., 9., 10., 11.)
( 12., 13., 14., 15., 16., 17.)]
# shape changed
print(yo_reloaded.shape)
(3,)
dtypefor genfromtxt. float is the default. Giving multiple dtypes tells it to load it as a structured array. Look at the dtype of the reload.