mardi 6 avril 2021

Setting a maximum number of replacements for sample with numpy choice

I want to select a random sample from an array with a maximum number of replacements.

Take the following example:

import numpy as np
a = np.arange(3)

rng = np.random.default_rng(seed=3)
rng.choice(a, 5)

This generates an array of [2, 0, 0, 0, 0].
Here, the element 0 has been selected four times.
Is there a way to limit this to e.g. two times?




Aucun commentaire:

Enregistrer un commentaire