如何在Python/numpy中将3D数组结果保存到4D数组中? [英] How can I save 3D array results to a 4D array in Python/numpy?

查看:577
本文介绍了如何在Python/numpy中将3D数组结果保存到4D数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关32 X 32 RGB图像的信息.所以它是一个形状为(32,32,3)的3D数组,第三维保持颜色R,G和B

I am reading the information about 32 X 32 RGB images. So it is a 3D array with shape (32,32,3) third dimension holds the color R, G and B

现在,我想读取50个这样的图像,并排列这些图像.因此,我决定制作一个尺寸为(50,32,32,3)的4D数组,其中第一维中的50是图像的数量,第二,第三和第四维中的图像是(32, 32,3)

Now, I want to read 50 such images and make a array of these images. So I decide to make a 4D array which will have dimension (50, 32, 32, 3) here 50 in first dimension is the number of images and the second, third and fourth dimension are the dimension of the image which are (32, 32, 3)

我尝试使用连接进行此操作,但出现错误.有什么办法吗?

I tried doing it using concatenate but I get errors. Is there any way to do this?

推荐答案

您需要在连接前添加一个轴,例如

You need to add an axis before concatenation, e.g.

import numpy as np
arrs = [np.random.random((32, 32, 3))
        for i in range(50)]

res = np.concatenate([arr[np.newaxis] for arr in arrs])
res.shape
# (50, 32, 32, 3)

或者,在这种情况下,您可以简单地在数组列表上调用np.array:

alternatively, in this case you can simply call np.array on your list of arrays:

res = np.array(arrs)
res.shape
# (50, 32, 32, 3)

这篇关于如何在Python/numpy中将3D数组结果保存到4D数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆