将numpy数组转换为2d数组 [英] converty numpy array of arrays to 2d array

查看:103
本文介绍了将numpy数组转换为2d数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫系列features,具有以下值(features.values)

I have a pandas series features that has the following values (features.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,)

而不是我期望的(10000, 3000).

如何将其识别为2d而不是将数组作为值的1d数组.还有为什么它不自动将其检测为2d数组?

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]])

结果是二维数组.

相反,如果我们创建一个对象dtype数组,并用数组填充它:

If instead we make an object dtype array, and fill it with arrays:

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)

请注意,此显示与您的显示类似.通过设计,这是一维数组.像列表一样,它包含指向内存中其他位置的数组的指针.请注意,这需要额外的构造步骤. np.array的默认行为是在可以的地方创建一个多维数组.

Notice that this display is like yours. This is, by design a 1d array. 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.

要解决这个问题需要花费额外的精力.同样,要撤消该操作也需要付出额外的努力-创建2d数字数组.

It takes extra effort to get around that. Likewise it takes some extra effort to undo that - to create the 2d numeric array.

仅在其上调用np.array不会更改结构.

Simply calling np.array on it does not change the structure.

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

stack确实将其更改为2d. stack将其视为数组列表,并在新轴上联接.

stack does change it to 2d. stack treats it as a list of arrays, which it joins on a new axis.

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

这篇关于将numpy数组转换为2d数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆