如何在numpy的二维数组对象DTYPE转换为花车常规二维数组数组、花车、转换为、常规

2023-09-08 09:19:27 作者:世界轮回

作为更广泛计划的一部分,我的工作,我结束了对象数组和字符串,3D坐标等所有的混合。我知道对象数组可能不是很喜欢在比较结构化阵列,但我希望能解决这个问题,而不改变了很多codeS。

As part of broader program I am working on, I ended up with object arrays with strings, 3D coordinates and etc all mixed. I know object arrays might not be very favorite in comparison to structured arrays but I am hoping to get around this without changing a lot of codes.

让我们假设我的阵列obj_array的每一行(N行)的格式

Lets assume every row of my array obj_array (with N rows) has format of

Single entry/object of obj_array:  ['NAME',[10.0,20.0,30.0],....] 

现在,我试图加载的对象的数组,切片的三维坐标数据块。到这里为止,一切都正常工作与简单的询问让说的。

Now, I am trying to load this object array and slice the 3D coordinate chunk. Up to here, everything works fine with simply asking lets say for .

obj_array[:,[1,2,3]]

不过结果也是一个对象数组,我将面临的问题,因为我想彩车组成的二维数组:

However the result is also an object array and I will face problem as I want to form a 2D array of floats with:

size [N,3] of N rows and 3 entries of X,Y,Z coordinates

现在,我的每一行遍历行并分配给行的目的地2D海军报阵列来解决这个问题。我想知道是否有一个与numpy的数组转换工具什么更好的办法?我尝试了一些东西,不能绕过它。

For now, I am looping over rows and assigning every row to a row of a destination 2D flot array to get around the problem. I am wondering if there is any better way with array conversion tools of numpy ? I tried a few things and could not get around it.

Centers   = np.zeros([N,3])

for row in range(obj_array.shape[0]):
    Centers[row,:] = obj_array[row,1]

感谢

推荐答案

讨厌的小问题...我一直打打闹闹与这个玩具的例子:

Nasty little problem... I have been fooling around with this toy example:

>>> arr = np.array([['one', [1, 2, 3]],['two', [4, 5, 6]]], dtype=np.object)
>>> arr
array([['one', [1, 2, 3]],
       ['two', [4, 5, 6]]], dtype=object)

我的第一个猜测是:

My first guess was:

>>> np.array(arr[:, 1])
array([[1, 2, 3], [4, 5, 6]], dtype=object)

不过,保持了对象 DTYPE,所以也许那么:

But that keeps the object dtype, so perhaps then:

>>> np.array(arr[:, 1], dtype=np.float)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.

您可以正常解决此执行下列操作:

You can normally work around this doing the following:

>>> np.array(arr[:, 1], dtype=[('', np.float)]*3).view(np.float).reshape(-1, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected a readable buffer object

不在这里虽然,那种这是令人费解。显然,这是一个事实,即在阵列中的对象都列出了引发这一关,与元组替换名单如下:

Not here though, which was kind of puzzling. Apparently it is the fact that the objects in your array are lists that throws this off, as replacing the lists with tuples works:

>>> np.array([tuple(j) for j in arr[:, 1]],
...          dtype=[('', np.float)]*3).view(np.float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

由于似乎没有任何令人完全满意的解决方案,最简单的可能是去的:

Since there doesn't seem to be any entirely satisfactory solution, the easiest is probably to go with:

>>> np.array(list(arr[:, 1]), dtype=np.float)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

虽然这不会是非常有效的,可能更好地去的东西,如:

Although that will not be very efficient, probably better to go with something like:

>>> np.fromiter((tuple(j) for j in arr[:, 1]), dtype=[('', np.float)]*3,
...             count=len(arr)).view(np.float).reshape(-1, 3)
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])