I am trying to output a 4D numpy float array to a plaintext file using numpy.savetxt
However numpy gives an error saying that a float argument is required when I try to pass this array. Nevertheless the numpy doc specifies that the argument to be passed should just be array like... NOT that it should be of max rank 2. The only way I can make it work is by reshaping the data to 2D (and this is actually not always practical for data organisation reasons)
Is there way around this? Or must one necessarily reshape the numpy array to 2D? I was expecting to be able to read the data in fortran like column-by-column style (working up through the dimensions).
Are there other possibilities? Note that I do not want to use the npy format since I seek compatibility with another program which needs plaintext format.
Creating arrays with more than one dimension In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.
The NumPy library contains multidimensional array and matrix data structures (you'll find more information about this in later sections). It provides ndarray, a homogeneous n-dimensional array object, with methods to efficiently operate on it.
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N positive integers that specify the sizes of each dimension.
Python Numpy – Create One Dimensional Array One dimensional array contains elements only in one dimension. In other words, the shape of the numpy array should contain only one value in the tuple. To create a one dimensional array in Numpy, you can use either of the array(), arange() or linspace() numpy functions.
If you look at the source code for numpy.savetxt
you'll find
for row in X:
fh.write(asbytes(format % tuple(row) + newline))
so numpy.savetxt
will only work for 1- or 2D-arrays.
For interoperability, you could use JSON if you have enough memory to convert the numpy array to a list:
import json
import numpy as np
a = np.arange(24).reshape(-1, 2, 3, 4).astype('float')
a[0,0,0,0] = np.nan
with open('/tmp/out', 'w') as f:
json.dump(a.tolist(), f, allow_nan = True)
yields
[[[[NaN, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0], [8.0, 9.0, 10.0, 11.0]], [[12.0, 13.0, 14.0, 15.0], [16.0, 17.0, 18.0, 19.0], [20.0, 21.0, 22.0, 23.0]]]]
A different approach is to save the array as a simple list of numbers (the flat version of the array) and save along it the information about its shape.
The problem about multidimensional arrays is that it's not that simple to move them from program to program even in text format.
you can do something like this:
myarray = rand(5,5,5)
name = 'myarray'+myarray.shape+'.txt'
np.savetxt(name,myarray.flatten())
and use the information on the size inclued in the filename to restore the initial shape
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