0

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.2, (23,1,32)

I wish to randomly sample one point based upon the fitness values. I decided the best way to do this would be to use numpy.random.choice(range(n), 1, plist[:,:1,:1])

However, i need to convert this into an numpy array, for which i tried

>> pArr = np.array( plist ) 
ValueError: setting an array element with a sequence

I got the same error for np.asarray(plist) as well.. any suggestions??

2
  • afaik np arrays must be of all elements of the same type... Commented Sep 3, 2012 at 20:08
  • Is it possible to convert it into a 3 dimensional array? or do i change the formation of this list itself to make it 3 dimensional? Commented Sep 3, 2012 at 20:11

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That does work thanks!! but does not really help in this situation. Since i am trying to slice out the fitness values for random selection. Which brings me back to the initial problem of slicing one dimension in a multi dimensional array ...!
1

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

2 Comments

Well, the fitness is different from the coordinates, and i would like to keep it that way.. But I guess i could do this and add 1 where ever i wish to access the coordinates!
The only problem being that, my dimensions are variable. So I may have (b,c,d) or (b,c,d,e...) in the tuple, can this be generalized?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.