jeudi 29 avril 2021

Weighted random sample from a list of lists in python

I would like to select multiple elements (lists) from a list but considering a weighted probability for each element, so far I've tried with np.random.choice but it sends an error that says that the list must be 1-dimensional.

import numpy as np

population = [[0, 7, 3, 3, 5, 0, 6], [0, 8, 4, 3, 5, 2, 5], [2, 1, 6, 6, 6, 2, 2], 
              [3, 6, 1, 7, 4, 3, 6], [3, 8, 1, 3, 6, 0, 5], [4, 7, 2, 0, 3, 5, 8], 
              [4, 8, 5, 5, 6, 2, 0], [1,0,0,1,0,1,1], [5, 1, 3, 4, 4, 4, 0], 
              [6, 7, 5, 5, 2, 3, 5], [7, 5, 3, 8, 3, 4, 2], [8, 1, 3, 5, 1, 5, 6], 
              [8, 1, 5, 7, 7, 5, 8], [8, 2, 4, 8, 7, 0, 8], [1,0,0,0,1,1,1], 
              [8, 4, 8, 2, 3, 5, 6], [8, 6, 3, 2, 4, 2, 2], [8, 7, 2, 8, 5, 2, 2]]

probability = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5833333333333334, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.4166666666666667, 0.0, 0.0, 0.0]

selection = np.random.choice(population,size=2,replace=False, p=probability)

print(selection)

>> ValueError: a must be 1-dimensional



Aucun commentaire:

Enregistrer un commentaire