简体   繁体   English

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

[英]converty numpy array of arrays to 2d array

I have a pandas series features that has the following values ( features.values )我有一个具有以下值的熊猫系列featuresfeatures.values

array([array([0, 0, 0, ..., 0, 0, 0]), array([0, 0, 0, ..., 0, 0, 0]),
       array([0, 0, 0, ..., 0, 0, 0]), ...,
       array([0, 0, 0, ..., 0, 0, 0]), array([0, 0, 0, ..., 0, 0, 0]),
       array([0, 0, 0, ..., 0, 0, 0])], dtype=object)

Now I really want this to be recognized as matrix, but if I do现在我真的希望它被识别为矩阵,但如果我这样做

>>> features.values.shape
(10000,)

rather than (10000, 3000) which is what I would expect.而不是(10000, 3000)这是我所期望的。

How can I get this to be recognized as 2d rather than a 1d array with arrays as values.我怎样才能让它被识别为二​​维而不是一个以数组为值的一维数组。 Also why does it not automatically detect it as a 2d array?另外为什么它不会自动将其检测为二维数组?

In response your comment question, let's compare 2 ways of creating an array 在回答您的评论问题时,让我们比较两种创建数组的方法

First make an array from a list of arrays (all same length): 首先从数组列表中创建一个数组(所有长度相同):

In [302]: arr = np.array([np.arange(3), np.arange(1,4), np.arange(10,13)])
In [303]: arr
Out[303]: 
array([[ 0,  1,  2],
       [ 1,  2,  3],
       [10, 11, 12]])

The result is a 2d array of numbers. 结果是一个2d数组。

If instead we make an object dtype array, and fill it with arrays: 相反,我们创建一个对象dtype数组,并用数组填充它:

In [304]: arr = np.empty(3,object)
In [305]: arr[:] = [np.arange(3), np.arange(1,4), np.arange(10,13)]
In [306]: arr
Out[306]: 
array([array([0, 1, 2]), array([1, 2, 3]), array([10, 11, 12])],
      dtype=object)

Notice that this display is like yours. 请注意,此显示与您的显示相同。 This is, by design a 1d array. 这是一个1d阵列。 Like a list it contains pointers to arrays elsewhere in memory. 像列表一样,它包含指向内存中其他位置的数组的指针。 Notice that it requires an extra construction step. 请注意,它需要额外的构造步骤。 The default behavior of np.array is to create a multidimensional array where it can. np.array的默认行为是创建一个多维数组。

It takes extra effort to get around that. 需要额外的努力来解决这个问题。 Likewise it takes some extra effort to undo that - to create the 2d numeric array. 同样,需要一些额外的努力才能撤消 - 创建二维数字数组。

Simply calling np.array on it does not change the structure. 只需在其上调用np.array就不会改变结构。

In [307]: np.array(arr)
Out[307]: 
array([array([0, 1, 2]), array([1, 2, 3]), array([10, 11, 12])],
      dtype=object)

stack does change it to 2d. stack确实将其更改为2d。 stack treats it as a list of arrays, which it joins on a new axis. stack将其视为一个数组列表,它将它连接到一个新轴上。

In [308]: np.stack(arr)
Out[308]: 
array([[ 0,  1,  2],
       [ 1,  2,  3],
       [10, 11, 12]])

缩短@hpauli 答案:

your_2d_arry = np.stack(arr_of_arr_object)

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

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