3d Random sampling [duplicate]

2019-07-14 19:05发布

问题:

This question already has an answer here:

  • How to efficiently get 10% of random numbers, then 10% of remaining 90 etc untill all points allocated 1 answer

I have a set of points xyz8,

I want to randomly get 10% of points.

Then I want to randomly get 10% of the remaining 90%

Then I want to randomly get 10% of the remaining 70%

etc until all points done

How can I go about doing this?

Any advice is hugely appreciated

回答1:

something like:

import random

l = [1,2,3,4]
random.shuffle(l)
while len(l) > 0:
    choice = l[:len(l) / 10]
    l = l[len(l) / 10:]


回答2:

I interpret this as you want to split the points into 10 equal-sized segments. You can simply do this by shuffling them and reshaping the list:

np.random.shuffle(points)
points.shape = (10,-1) + points.shape[1:]

Then you can access the first 10% as points[0], the second as points[1], etc.

This still works for a multidimensional array since shuffle will only shuffle along the first axis.