1

Given that i have a dataset as below:

dict = {
    "No": list(range(1,9)),
    "Pro":  [0.6703557312252965, 0.20395256916996046, 0.0782608695652174, 0.03241106719367589, 0.009486166007905139, 0.0031620553359683794, 0.0015810276679841897, 0.0007905138339920949]
}

dt = pd.DataFrame(dict)

And i need to generate a number from "NO" column based on "Pro" column:

noList = dt["No"].tolist()
pro = dt["Pro"].tolist()
print("NoList", noList, " Pro: ", pro)
randomPolypNo = np.random.choice(noList, pro)

But, it complains with:

TypeError: 'float' object cannot be interpreted as an integer

What is the problem?

1
  • 1
    From the docs: parameter 2 is size, not the weights list. Try: np.random.choice(dt["No"].tolist(), 1, p=dt["Pro"].tolist()) Commented Jun 11, 2020 at 20:47

2 Answers 2

2

I think you need to specify that pro is your probability distribution.

randomPolypNo = np.random.choice(noList, p=pro)

As is, it's interpreting pro as the size of the desired sample, which needs to be an int.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to specify the arguments. Did you mean "Pro" as the probabilities? You can solve it by this.

randomPolypNo = np.random.choice(a=noList, p=pro)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.