0

This code is to randomize an array with a specific proportion

a0 = np.full((17),0)
a1 = np.full((30),1)
a2 = np.full((38),2)
a3 = np.full((15),3)
options = np.concatenate((a0,a1,a2,a3))
np.random.shuffle(options)

it works as expected generating [a0, a1, a2, a3] with the specific proportion [17%, 30%, 38%, 15%]

However, it becomes ugly when the number of "ax" grow large, like [a0, a1, ... , a999] with [1%, 5%, ... , .2%]

Does Python or NumPy have API to do that work?

2 Answers 2

2

You can use a dict where the key is number and the value is the repeats. Then use a list comprehension to build the list to concatenate.

d = {
0: 17,
1: 30,
2: 38,
3: 15
}

options = no.concatenate([np.full((v), k) for k, v in d.items()])
Sign up to request clarification or add additional context in comments.

Comments

0

So if you grow the basenumber the individual percentage decreases, if you want to hold the same percentages you have to increment the size of the arrays with the same proportion. Instead of writing '17' you could try to write 0.17 * len(total array), if this does help

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.