About the np.random.choice()

Hi,
I found something wrong in the assignment ‘Dinosaurus_Island_Character_level_language_model’ I doubt.

Example of how to use np.random.choice() :
np.random.seed(0)
probs = np.array([0.1, 0.0, 0.7, 0.2])
idx = np.random.choice(range(len(probs), p = probs))

I think it should be … idx = np.random.choice(range(len(probs)), p = probs)

right? I am not sure.

thank you

You want the index to be sampled according to a certain weight and not using uniform distribution. So, using p=probs is correct. See this link

Your suggestion should produce a compilation error since there’s an extra closing bracket after p=probs

Thanks.
I am confused about it for a long time because it’s in the ’ Additional Hints’. :joy:

Hope things are sorted out now. Have fun.

Its the other way round for me. The suggestion by @jason1 worked fine and the hint in the assignment did not. It threw the error “range() takes no keyword arguments”. Could you explain what’s going on ?

Thanks in Advance

Please share the block where you’re observing this error. Thanks.

From the instruction, the code idx = np.random.choice(range(len(probs), p = probs)) throws error

where as what @jason1 suggested idx = np.random.choice(range(len(probs)), p = probs) runs without error. (I mean just this line of code, not the assignment block)

So I couldn’t understand from what you meant by Your suggestion should produce a compilation error since there’s an extra closing bracket after p=probs

Your code is correct. The closing brackets should match.