mardi 26 mars 2019

Is there a function in Python which gives an output based on a probability distribution?

I have an array of numbers, say from 0 to 8, which may appear repetedly in the array. I need to choose a number among those, and the probability of a number coming out should be proportional to the number of times it appears in that array.

This is the original array: ([7, 0, 7, 8, 4, 4, 6, 5, 2, 6, 0, 1, 2, 3, 4, 5, 6, 7, 8])

This is the array containing the number of times each number appears in the array:
array([ 2., 1., 3., 1., 1., 4., 1., 5., 1.])

This is the code in which I tried to get one index (of deg) in the way I described before

        n = np.random.uniform(0, tot)
        for i in range(len(deg)):
            if n < deg[i]:
                return i
            n = n - deg[i]
        return i

I get an index, 2, but I don't know if the process is probabilitistically correct. What do you say?




Aucun commentaire:

Enregistrer un commentaire