Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a 3D numpy array to .txt file

Tags:

python

save

numpy

I'm just asking the question because it worked two days ago on my desktop, but as I'm trying to run this on my laptop, it doesn't work.

array = numpy.zeros((3,3,1))
numpy.savetxt("nxx", array, fmt='%s')

This gives me the following error:

"Expected 1D or 2D array, got %dD array instead" % X.ndim) ValueError: Expected 1D or 2D array, got 3D array instead

like image 368
Lucas Sierota Avatar asked Jul 07 '18 00:07

Lucas Sierota


2 Answers

As the docs explain, savetxt can only save 1D and 2D arrays.

And how would you want it to be saved? savetxt saves into a CSV file: there are columns separated by whitespace, and rows separated by newlines, which looks nice and 2D in a text editor. How would you extend that to 3D?

If you have some specific format in mind that you want to use, there may be a formatter for that (or maybe it's just a matter of reshape-ing the array down to 2D?), but you'll have to explain what it is that you want.

If you don't care about editing the output in a text editor, why not just save it in binary format, e.g., numpy.save("nxx.npy", array)? That can handle any shape just fine.


IIRC, old versions of NumPy handled this by flattening all but one of the dimensions down into a single dimension. So, to use your data, you'd have to load it up and then reshape it—which you could only do if you knew, from somewhere else, what the original dimensions were. And you wouldn't even realize the problem until you'd spent all week generating a few TB of files that you now couldn't figure out how to use. (I'd guess this is why they changed the behavior—it was used by accident more often than on purpose, and with annoying consequences.)

Anyway, if you want that behavior, it's of course trivial to rehape the array to 2D yourself and then save that:

np.savetxt("nxx", array.reshape((3,-1)), fmt="%s")

Or, even better:

np.savetxt("nxx", array.reshape((3,-1)), fmt="%s", header=str(array.shape))
like image 148
abarnert Avatar answered Oct 12 '22 01:10

abarnert


Wanted to give some additional code snippets to provide more intuition. It's fine writing a 2D array as follows.

b = np.array([[1, 2], [3, 4]])

np.savetxt("racoon.txt", b)

Writing a 3D array will cause the error you mentioned.

c = np.array([[[1, 2], [3, 4]]])

np.savetxt("fox.txt", c)

3D arrays can we written an binary NumPy files, as abarnert mentioned:

np.save("parrot.npy", c)

This example illustrates one of the reasons why text files are not great for storing array data. Binary files like Zarr are better for many reasons, including being able to seamlessly handle 3D+ array data.

like image 42
Powers Avatar answered Oct 12 '22 01:10

Powers