简体   繁体   English

将列表列表转换为二维 numpy 数组

[英]Converting a list of lists into a 2D numpy array

I have a list X which contains a list of lists of lists... eg我有一个列表 X,其中包含一个列表列表......例如

X = [ [[], [], [], []], [[], [] ,[], []] ]

When i try to convert this list above (X) into a numpy array using np.array() , I get the following:当我尝试使用np.array()将上面 (X) 的这个列表转换为一个 numpy 数组时,我得到以下信息:

array([list([[],[],[],[]]),
list([list([[],[],[],[]]),....) 

and the list goes on pun intended*.名单上的双关语是*。

What am I doing wrong here?我在这里做错了什么? I just need it in a normal array form such that I can index it in ways lists don't allow (ie array[:,1] )我只需要一个普通的数组形式,这样我就可以以列表不允许的方式索引它(即array[:,1]

If your lists are NOT of the same length (in each nested dimension) you CANT do a traditional conversion to a NumPy array because it's necessary for a NumPy array of 2D or above to have the same number of elements in its first dimension.如果您的列表长度不同(在每个嵌套维度中),您不能对 NumPy 数组进行传统转换,因为二维或更高版本的 NumPy 数组必须在其第一维中具有相同数量的元素。

So you cant convert [[1,2],[3,4,5]] to a numpy array directly.所以你不能直接将[[1,2],[3,4,5]]转换为 numpy 数组。 Applying np.array will give you a 2 element numpy array where each element is a list object as - array([list([1, 2]), list([3, 4, 5])], dtype=object) .应用np.array将为您提供一个 2 元素的 numpy 数组,其中每个元素都是一个列表对象,如 - array([list([1, 2]), list([3, 4, 5])], dtype=object) I believe this is the issue you are facing.我相信这就是您面临的问题。

You cant create a 2D matrix for example that looks like -你不能创建一个 2D 矩阵,例如看起来像 -

[[1,2,3,?],
 [4,5,6,7]]

What you may need to do is pad the elements of each list of lists of lists to a fixed length (equal lengths for each dimension) before converting to a NumPy array.您可能需要做的是在转换为 NumPy 数组之前将每个列表列表的元素填充到固定长度(每个维度的长度相等)。

I would recommend iterating over each of the lists of lists of lists as done in the code I have written below to flatten your data, then transforming it the way you want.我建议按照我在下面编写的代码中所做的那样迭代每个列表列表,以扁平化您的数据,然后按照您想要的方式对其进行转换。


If your lists are of the same length, then should not be a problem with numpy version 1.18.5 or above.如果您的列表长度相同,那么 numpy 1.18.5或更高版本应该没有问题。

a = [[[1,2],[3,4]],[[5,6],[7,8]]]
np.array(a)
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])

However, if you are unable to still work with the list of list of lists, then you may need to iterate over each element first to flatten the list and then change it into a numpy array with the required shape as below -但是,如果您仍然无法使用列表列表,那么您可能需要先遍历每个元素以展平列表,然后将其更改为具有所需形状的 numpy 数组,如下所示 -

a = [[[1,2],[3,4]],[[5,6],[7,8]]]
flat_a = [item for sublist in a for subsublist in sublist for item in subsublist]
np.array(flat_a).reshape(2,2,2)
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])

Try this:尝试这个:

>>> import numpy as np
>>> a = np.array([[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]])
>>> a
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]]])
>>> a.reshape(4,-1)
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])


>>> a.reshape(1,-1)
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]])

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

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