Stefano Kira
Stefano Kira

Reputation: 175

Probabilities are ignored in numpy

I'm trying to use the numpy library to sample a character from a distribution, but it seems to ignore the probabilities I give in input. I have a probability array, which just to test I set to

vec_p=[0,0,1,0,0] 

and a character array

vec_c=[a,b,c,d,e]

If I do

numpy.random.choice(vec_c,10,vec_p)

I would expect to get

cccccccccc

since the other probabilities are all zero, but it just gives me random values ignoring the vec_p array. Am I doing something wrong?

Upvotes: 1

Views: 53

Answers (1)

Moses Koledoye
Moses Koledoye

Reputation: 78556

Passing the parameters as keyword arguments gives the correct results:

>>> import numpy as np
>>> vec_p = [0,0,1,0,0]
>>> num = np.arange(5)
>>> np.random.choice(num, size=10, p=vec_p)
array([2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

Upvotes: 2

Related Questions