uhoh
uhoh

Reputation: 3745

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

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.

Upvotes: 0

Views: 189

Answers (1)

Stef
Stef

Reputation: 30639

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

Upvotes: 1

Related Questions