This seems like a simple enough task, but I haven't found how to do it using numpy
. Consider the example array:
这似乎是一个足够简单的任务,但我还没有找到如何使用numpy来做到这一点。考虑示例数组:
import numpy as np
aa = np.array([np.array([13.16]), np.array([1.58 , 1.2]), np.array([13.1]), np.array([1. , 2.6])], dtype=object)
I need a general way to flatten that array into a single array of N
elements, with N=every float in all the sub-arrays
. In this case it would be:
我需要一种通用的方法将该数组展平为N个元素的单个数组,其中N =所有子数组中的每个浮点数。在这种情况下,它将是:
aa = np.array([13.16, 1.58 , 1.2, 13.1, 1. , 2.6])
I've tried np.ndarray.flatten()
(tried all the 'order' options)) but I get back the same unchanged aa
array.
我已经尝试了np.ndarray.flatten()(尝试了所有'order'选项))但是我得到了相同的未更改的aa数组。
Why is np.ndarray.flatten()
not working and how can I accomplish this?
为什么np.ndarray.flatten()不起作用,我怎么能做到这一点?
The solution should be as general as possible since the example aa
array I'm using here will actually be filled with sub-arrays of different lengths in my real code.
解决方案应该尽可能通用,因为我在这里使用的数组实例将在我的实际代码中填充不同长度的子数组。
1 个解决方案
#1
1
You can use numpy.hstack
你可以使用numpy.hstack
>>> np.hstack(aa)
array([13.16, 1.58, 1.2 , 13.1 , 1. , 2.6 ])
#1
1
You can use numpy.hstack
你可以使用numpy.hstack
>>> np.hstack(aa)
array([13.16, 1.58, 1.2 , 13.1 , 1. , 2.6 ])