简体   繁体   English

如何在Python中将不同形状的数组列表转换为numpy数组

[英]how to convert list of different shape of arrays to numpy array in Python

I have a different shape of 3D matrices. 我有不同形状的3D矩阵。 Such as: 如:

  1. Matrix shape = [5,10,2048] 矩阵形状= [5,10,2048]
  2. Matrix shape = [5,6,2048] 矩阵形状= [5,6,2048]
  3. Matrix shape = [5,1,2048] 矩阵形状= [5,1,2048]

and so on.... 等等....

I would like to put them into big matrix, but I am normally getting a shape error (since they have different shape) when I am trying to use numpy.asarray(list_of_matrix) function. 我想将它们放入大矩阵,但是当我尝试使用numpy.asarray(list_of_matrix)函数时,通常会出现形状错误(因为它们具有不同的形状)。

What would be your recommendation to handle such a case? 您对处理这种情况有何建议?

My implementation was like the following: 我的实现如下所示:

matrices = []
matrices.append(mat1)
matrices.append(mat2)
matrices.append(mat3)
result_matrix = numpy.asarray(matrices)

and having shape error!! 并有形状错误!

UPDATE 更新

I am willing to have a result matrix that is 4D. 我愿意有一个4D的结果矩阵。

Thank you. 谢谢。

I'm not entirely certain if this would work for you, but it looks as though your matrices only disagree along the 1st axis, so why not concatenate them: 我不确定这是否适用于您,但似乎您的矩阵仅沿第一轴不一致,因此为什么不将它们连接起来:

eg 例如

>>> import numpy as np
>>> c=np.zeros((5,10,2048))
>>> d=np.zeros((5,6,2048))
>>> e=np.zeros((5,1,2048))
>>> f=np.concatenate((c,d,e),axis=1)
>>> f.shape
(5, 17, 2048)

Now, you'd have to keep track of which indices of the 1st axis corresponds to which matrices, but maybe this could work for you? 现在,您必须跟踪第一轴的哪些索引对应于哪些矩阵,但这也许对您有用吗?

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

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