Skip to content Skip to sidebar Skip to footer

Converting A List Of Ints, Tuples Into An Numpy Array

I have a list of [float, (float,float,float..) ] ... Which is basically an n-dimensional point along with a fitness value for each point. For eg. 4.3, (2,3,4) 3.2, (1,3,5) . . 48

Solution 1:

The following should work:

A = np.array([tuple(i) for i in initial_list],dtype=[('fitness',float),('point',(float,3))])

with initial_list = [[4.3, (2, 3, 4)], [3.2, (1, 3, 5)], ...]. Note that we need to transform each item of initial_list into a tuple for that trick to work, else NumPy cannot recognize the structure.

Your fitness entries are now accessible as A['fitness'], with the corresponding points as A['point']. If you select a list of actual fitness entries, indices, the corresponding points are given by A['point'][indices], which is a simple (n,3) array.


Solution 2:

Your question is difficult to understand. Is this what you're trying to do?

>>> x
[[4.3, (2, 3, 4)], [3.2, (1, 3, 5)], [48.2, (23, 1, 32)]]
>>> np.array([(a, b, c, d) for a, (b, c, d) in x])
array([[  4.3,   2. ,   3. ,   4. ],
       [  3.2,   1. ,   3. ,   5. ],
       [ 48.2,  23. ,   1. ,  32. ]])

Post a Comment for "Converting A List Of Ints, Tuples Into An Numpy Array"