简体   繁体   English

将 numpy arrays 的列表转换为 numpy 数组,其中元素为 numpy arrays

[英]convert a list of numpy arrays into a numpy array in which elements are numpy arrays

Python code to convert a list of numpy arrays into a numpy array having numpy arrays as its elements? Python 代码将 numpy arrays 的列表转换为具有 numpy arrays 作为其元素的 numpy 数组?

I have a list of arrays as below:我有一个 arrays 的列表如下:

my_list

Output: Output:

[array([1,2,3]),
    array(['a', 'b','c]),
    array(['text1', 'text2', 'text3'])
    ]

using np.asarray() converts the elements to list like the one shown below:使用 np.asarray() 将元素转换为如下所示的列表:

my_array = np.asarray(my_list)

my_array looks like: my_array 看起来像:

array([[1,2,3],['a', 'b','c], ['text1', 'text2', 'text3']])

The kind of output that I want:我要的那种output:

array([array([1,2,3]),array(['a', 'b','c]),array(['text1', 'text2', 'text3'])])

Can someone please help me with the code?有人可以帮我处理代码吗?

In [173]: alist = [np.array([1,2,3]),
     ...:     np.array(['a', 'b','c']),
     ...:     np.array(['text1', 'text2', 'text3'])
     ...:     ]
In [174]: alist
Out[174]: 
[array([1, 2, 3]),
 array(['a', 'b', 'c'], dtype='<U1'),
 array(['text1', 'text2', 'text3'], dtype='<U5')]
In [175]: np.array(alist)
Out[175]: 
array([['1', '2', '3'],
       ['a', 'b', 'c'],
       ['text1', 'text2', 'text3']], dtype='<U21')

np.array tries to make as high of a multidimensional array as it can. np.array尝试制作尽可能高的多维数组。 The 3 arrays of the same size it can make a (3,3).同样大小的3个arrays可以拼成一个(3,3)。

To avoid that we have to create a target array of the desired shape and dtype, and assign the values from the list:为避免这种情况,我们必须创建一个具有所需形状和数据类型的目标数组,并从列表中分配值:

In [178]: arr = np.empty(3, object)
In [179]: arr[:] = alist
In [180]: arr
Out[180]: 
array([array([1, 2, 3]), array(['a', 'b', 'c'], dtype='<U1'),
       array(['text1', 'text2', 'text3'], dtype='<U5')], dtype=object)

If the list contains arrays (or other objects) of a different size, np.array falls back to making an object dtype array of those items.如果列表包含不同大小的 arrays(或其他对象), np.array回退到制作这些项目的 object dtype 数组。 But pay attention to the warning, and keep in mind that this is a fallback option, and as you found, not the default behavior.但请注意警告,并记住这是一个回退选项,正如您所发现的,这不是默认行为。

In [181]: alist.append([1,2])
In [182]: np.array(alist)
<ipython-input-182-7512d762195a>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  np.array(alist)
Out[182]: 
array([array([1, 2, 3]), array(['a', 'b', 'c'], dtype='<U1'),
       array(['text1', 'text2', 'text3'], dtype='<U5'), list([1, 2])],
      dtype=object)

And to repeat my comment - why do you want an array of arrays?并重复我的评论 - 为什么你想要一个 arrays 的数组? What advantage does that have compared to the original list?与原始列表相比,它有什么优势?

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

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