Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can numpy.savetxt be used on N-dimensional ndarrays with N>2?

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.

like image 692
Mathias Vanwolleghem Avatar asked Nov 21 '12 14:11

Mathias Vanwolleghem


People also ask

Can NumPy arrays have more than 2 dimensions?

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.

Does NumPy library contains N-dimensional array object?

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.

How do you define in NumPy is an N-dimensional array?

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.

Is NumPy one dimensional array?

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.


2 Answers

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]]]]
like image 101
unutbu Avatar answered Oct 07 '22 04:10

unutbu


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

like image 21
EnricoGiampieri Avatar answered Oct 07 '22 05:10

EnricoGiampieri