0

I have run into a wall here. So i pull random sample of, lets say 10 elements, and i want to give them probabilities, based on some condition. Lets say that i have this random sample [1, 3, 5, 7, 13, 16, 6, 9, 22, 15] so i want to give each number a probability based on the relative distance from int of 8. Further element is from 8, less the probability. In this case 7 and nine have same, biggest probabilities and 22 have the smallest?

I tried to find some numpy function, but with no success.

Thanks!

  • What have you tried so far? (code speaking) – diegoiva Aug 31 '18 at 12:59
  • Try and look at the answers here: https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice – unddoch Aug 31 '18 at 12:59
  • 1
    Possible duplicate of [A weighted version of random.choice](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice) – Joel Aug 31 '18 at 13:05

1 Answers1

1

You have to do two things. Create the probability, and select according to it. To create it

data = np.array([1, 3, 5, 7, 13, 16, 6, 9, 22, 15])
p = 1 / np.abs(data - 8)

(that's just and idea, you don't specify exactly how you want the probability to change with distance)

For the second part, numpy.choice function (in recent numpy versions) can accept the array p to select with that probability from data

blue_note
  • 27,712
  • 9
  • 72
  • 90