Lets say I have a file with the following values:
1 2
3 4
11 12
13 14
and I want to read them as a numpy 2x2x2 array. The standard command np.loadtxt('testfile')
reads them in as lots of vectors ignoring the spaces (4x1x8). I suppose I could iterate over them and stack them together in the right way, but my actual data files are pretty large and would rather not have too many while loops if possible. Is there a nice way to do this within the numpy system?
Thanks for your help!
Use reshape.
>>> import numpy
>>> a = numpy.loadtxt('testfile')
>>> a
array([[ 1., 2.],
[ 3., 4.],
[ 11., 12.],
[ 13., 14.]])
>>> a.reshape((2, 2, 2))
array([[[ 1., 2.],
[ 3., 4.]],
[[ 11., 12.],
[ 13., 14.]]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With