I have a table that looks like this:
view weight
A 1
B 1
C 2
D 1
E 1
F 1
G 3
I would like to sample from this table, but use the weight
column to determine the probability of being chosen. For the above table, A, B, D, E, and F would all be chosen with probability 0.1, C would be with probability 0.2 and G would be probability 0.3
Answers with python or SQL would be great.
2
Answers
It can be done with mysql using something like
Although I haven’t tested it. I’d always prefer to to it in python, as I find it much more readable and extendable. In python, you can use numpy’s random.choice
I got the following code to work
You can accomplish this by multiplying each row by its respective weight, and then randomly selecting a single row, so a view assigned a weight of 3 will be represented three times, increasing its likelihood of being selected.:
Demo here