简体   繁体   English

在数组数组中重塑数组

[英]Reshaping arrays in an array of arrays

I have an array of 40 arrays, each with a 1x150 shape. 我有40个阵列的阵列,每个阵列的形状均为1x150。 Is there a way to reshape the arrays so that I have 40 arrays of 3x50 arrays? 有没有办法改变阵列的形状,以便我有40个3x50阵列?

I am not sure if there is a way to use np.reshape and just do it in one line, is there? 我不确定是否有一种方法可以使用np.reshape并只在一行中执行,有吗?

Is this really an array of np.arrays, or a list of those arrays? 这真的是np.arrays array ,还是这些数组的列表? If it is an array, what is its shape and dtype? 如果是数组,它的形状和dtype是什么?

If it is a list, or array with dtype=object , then you have to iterate over items, and reshape each one. 如果它是列表或具有dtype=object数组,那么您必须遍历所有项,并调整每个项的形状。

 [a.reshape(3,50) for a in A]

If you have a 3d array, its shape may be (40, 1, 150) . 如果您具有3d数组,则其形状可能是(40, 1, 150)

 A.reshape(40, 3, 50)

Since the items in an 'object' array could be anything - strings, arrays, lists, dict - there can't be a reshape that applies to all of them 'at once'. 由于“对象”数组中的项目可以是任何内容-字符串,数组,列表,字典-不能一次“ reshape地应用于所有对象。 Even if they are all arrays, they could each have different dimensions. 即使它们都是数组,它们也可以具有不同的尺寸。 In fact that is usually how an array of arrays is produced. 实际上,通常就是如何生成数组的数组。

In [5]: np.array([[1,2,3],[2,3]])
Out[5]: array([[1, 2, 3], [2, 3]], dtype=object)

You have to take special steps to construct an object array with items that all have the same shape. 您必须采取特殊的步骤来构造具有相同形状的项目的对象数组。 np.array tries to construct the highest dimensional array it can. np.array尝试构造它可以的最高维数组。

In [7]: A=np.empty((2,),dtype=object)
In [8]: A[0] = np.array([1,2,3])
In [9]: A[1] = np.array([4,5,6])
In [10]: A
Out[10]: array([array([1, 2, 3]), array([4, 5, 6])], dtype=object)

Another way to look at it: reshape just changes an attribute of the array. 看待它的另一种方式: reshape只是更改数组的属性。 It does nothing to the data. 它对数据没有任何作用。 In the case of a 3d array, there is one shape value, and one block of data. 对于3d数组,只有一个shape值和一个数据块。

But in the array of objects, each object has its own shape and data . 但是在对象数组中,每个对象都有自己的shapedata

为了重塑包含numpy-arrays的numpy-array,我发现此贡献很有帮助-您可以首先使用b=np.hstack(array_of_arrays)创建一个扁平的一维numpy-array,然后重塑b

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

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