简体   繁体   English

将 3D numpy 数组保存到 .txt 文件

[英]Saving a 3D numpy array to .txt file

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 “预期的 1D 或 2D 数组,改为 %dD 数组”% X.ndim) ValueError: 预期的 1D 或 2D 数组,改为得到 3D 数组

As the docs explain, savetxt can only save 1D and 2D arrays. 正如文档解释的那样, savetxt只能保存1D和2D数组。

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. savetxt保存到CSV文件中: savetxt空格分隔的列,用换行符分隔的行,在文本编辑器中看起来不错,2D。 How would you extend that to 3D? 你会如何将其扩展到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. 如果您想要使用某种特定的格式,可能会有一个格式化程序(或者可能只是将数组reshape为2D?),但您必须解释它是什么你要的那个。

If you don't care about editing the output in a text editor, why not just save it in binary format, eg, numpy.save("nxx.npy", array) ? 如果您不关心在文本编辑器中编辑输出,为什么不直接将其保存为二进制格式,例如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. IIRC,NumPy的旧版本通过将除了一个维度之外的所有维度平面化为单个维度来处理此问题。 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. 你甚至不会意识到这个问题,直到你花了整整一周产生几TB的文件,你现在无法弄清楚如何使用。 (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: 无论如何,如果你想要这种行为,你自己将数组重新设置为2D然后保存它当然是微不足道的:

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))

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.编写 3D 数组会导致您提到的错误。

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

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

3D arrays can we written an binary NumPy files, as abarnert mentioned: 3D 数组我们可以写一个二进制 NumPy 文件,正如 abarnert 提到的:

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.Zarr这样的二进制文件更好的原因有很多,包括能够无缝处理 3D+ 数组数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM