How to build a numpy structured array with 2 int columns and 3 float columns?

China☆狼群 提交于 2019-12-11 14:52:46

问题


I have data which is a list of 5-tuples. The first two are integer indices i, j and the next three are floats xyz.

data = [(1, 2, 3.141, 1.414, 2.718), 
        (3, 4, 1.111, 2.222, 3.333),
        (0, 0, 0.000, 0.000, 0.000)]

I have heard that I can do something like

dt    = [('ij', 'int', 2), ('xyz', 'float', 3)]

struct_array = np.array(data, dtype=dt)

so I can access the last three columns of the array as a 2D float array. For example to get r = sqrt(x^2 + y^2 + z^2) I should be able to say

r = np.sqrt(((struct_array['xyz']**2).sum(axis=1)))

and get the result

array([4.38780139, 4.15698136, 0.        ])

the same way that

normal_array = np.array(data)

r = np.sqrt(((array[:, 2:]**2).sum(axis=1)))

but everything I've tried results in the error message

ValueError: could not assign tuple of length 5 to structure with 2 fields.

I've looked at https://docs.scipy.org/doc/numpy/user/basics.rec.html but if the answer to why my attempt fails is there I'm not seeing it.


回答1:


You'll have to pack the data into 2 tuples for the two elements of your structure:

struct_array = np.array([((e[0],e[1]), (e[2],e[3],e[4])) for e in data], dtype=dt)
np.sqrt(((struct_array['xyz']**2).sum(axis=1)))

Result

array([4.38780139, 4.15698136, 0.        ])


来源:https://stackoverflow.com/questions/59135931/how-to-build-a-numpy-structured-array-with-2-int-columns-and-3-float-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!