Python 无法展开numpy数组
我有一个包含图像信息的数组。它包含关于名为“shuffled”的数组中的21495个图像的信息Python 无法展开numpy数组,python,arrays,numpy,Python,Arrays,Numpy,我有一个包含图像信息的数组。它包含关于名为“shuffled”的数组中的21495个图像的信息 np.shape(shuffled) = (21495, 1) np.shape(shuffled[0]) = (1,) np.shape(shuffled[0][0]) = (128, 128, 3) # (These are the image dimensions, with 3 channels of RGB) 如何将此数组转换为形状数组(21495、128、128、3)以提供给我的模型?我
np.shape(shuffled) = (21495, 1)
np.shape(shuffled[0]) = (1,)
np.shape(shuffled[0][0]) = (128, 128, 3) # (These are the image dimensions, with 3 channels of RGB)
如何将此数组转换为形状数组(21495、128、128、3)以提供给我的模型?我可以想到两种方法: 一种是使用numpy的
vstack()
功能,但是当数组的大小开始增加时,它会变得非常缓慢
另一种方法(我使用)是获取一个空列表,并使用.append()
将图像数组附加到该列表中,然后最终将该列表转换为numpy数组。试试看
np.stack(shuffled[:,0])
堆栈
,一种形式的连接
,在新的初始维度上连接数组列表(或数组)。我们需要先去掉尺寸为1的维度
In [23]: arr = np.empty((4,1),object)
In [24]: for i in range(4): arr[i,0] = np.arange(i,i+6).reshape(2,3)
In [25]: arr
Out[25]:
array([[array([[0, 1, 2],
[3, 4, 5]])],
[array([[1, 2, 3],
[4, 5, 6]])],
[array([[2, 3, 4],
[5, 6, 7]])],
[array([[3, 4, 5],
[6, 7, 8]])]], dtype=object)
In [26]: arr.shape
Out[26]: (4, 1)
In [27]: arr[0,0].shape
Out[27]: (2, 3)
In [28]: np.stack(arr[:,0])
Out[28]:
array([[[0, 1, 2],
[3, 4, 5]],
[[1, 2, 3],
[4, 5, 6]],
[[2, 3, 4],
[5, 6, 7]],
[[3, 4, 5],
[6, 7, 8]]])
In [29]: _.shape
Out[29]: (4, 2, 3)
但是要注意,如果子阵列的形状不同,比如说一个或两个是b/w而不是3个通道,这将不起作用。你能发布阵列
。数据类型吗?你确定所有图像的形状都相同吗?