简体   繁体   English

如何将整数与np.array一起保存

[英]How to save integer alongside of a np.array

I am doing a ConvNet and need to save one image alongside an integer (1 or 0), that indicates if an action was taken or not. 我正在做一个ConvNet,需要将一个图像与一个整数(1或0)一起保存,以指示是否已执行操作。 How can I do it? 我该怎么做? I tried this: 我尝试了这个:

key_pressed = np.array(key_check(self.key)) # key_check() returns 1 or 0
window = np.array(window) # window is opened with cv2

print(window.shape, key_pressed.shape) # (474, 31) (1,)
print(np.concatenate((window, key_pressed)))
      ^^
ValueError: all the input arrays must have same number of dimensions

Is doing what I want even possible with numpy, seeing that the arrays are of different sizes? 看到数组的大小不同,用numpy做我想做的事吗? Any ideas on how to save this data? 关于如何保存此数据的任何想法?

NumPy won't let you simply concatenate things of arbitrary sizes: that breaks the functional definition of an array or matrix. NumPy不会让您简单地将任意大小的东西连接起来:这破坏了数组或矩阵的功能定义。 You're trying to make a simple juxtaposition, not a functional concatenation. 您正在尝试进行简单的并置,而不是功能串联。 All you're trying to do is display them side by side, right? 您要做的就是将它们并排显示,对吗? This isn't a matrix operation. 这不是矩阵运算。 For instance, do you really envision doing a matrix multiplication on the resulting glued array??? 例如,您是否真的设想对所得的粘合数组进行矩阵乘法???

You need a simple container to hold both items. 您需要一个简单的容器来容纳两个项目。 I expect that a trivial class will do the job for you. 我希望一个简单的class能为您完成这项工作。 Write a combined display method for the resulting pair, and use that for your rendering. 为结果对编写一个组合的显示方法,并将其用于渲染。

Have a look at scipy.io.savemat and loadmat : 看看scipy.io.savematloadmat

savemat(your_path, dict(key=key_pressed, window=window))

Then using loadmat you'll get back a dict with the key and the frame that you want. 然后使用loadmat返回带有指定键和所需帧的字典。 There is even a do_compression parameter. 甚至还有一个do_compression参数。

Edit: the incompatibility in shapes have nothing to do with it, since you're not creating a new array but saving key and window separately in the same file. 编辑:形状不兼容与它无关,因为您不是在创建新数组,而是将键和窗口分别保存在同一文件中。

In [53]: from scipy import io

In [54]: a = np.zeros([3, 4])

In [55]: b = np.ones([5, 17])

In [56]: io.savemat("foo.mat", dict(a=a, b=b))

it works like a charm. 它就像一个魅力。

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

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