I want to generate a random numpy ndarray of 0 and 1. I want that the number of occurrences of 1 in every specific rowto range between 2 and 5. I tried:x = np.random.randint(0,2, size=(10, 10))yet i cannot control the number of ones. I tried the np. random.choice() but we only can control the probability of the number. for instance, 1 will be 0.2 of the array. Yet, I want this probability to vary and be in a specific range. I also tried this code for every row in of my ndarray. one_count = np.random.randint(2, 5))

zero_count = colnumber - one_count

my_array = [0] * zero_count + [1] * one_count


Can you please help me in finding a better solution?


1 Answer 1


It ultimately depends on the nature of the underlying data for your simulation to represent that. For instance, if you have some type of censored Poisson process what I will show won't make sense as is.

But, one way is to generate all possible permutations that meet your criteria (here there end up being 627 possible permutations that meet {10 choose 2} + {10 choose 3} ... + {10 choose 5}). And then you can sample at random from that greater choice set.

import itertools as it
import numpy as np

# Lets create the whole sets of possible permutation lists
res = []
zr = np.zeros(10)
for i in range(2,6):
    for p in it.combinations(range(10),i):
        on = zr.copy()
        on[list(p)] = 1

resnp = np.stack(res,axis=0)

# Now lets sample 1000 from this list
total_perms = resnp.shape[0]
samp = np.random.choice(total_perms,1000)
res_samp = resnp[samp]

# Check to make sure this is OK

If you had observed data, you could generate probabilities from that observed data and feed into the p probability argument for np.random.choice.

In this scenario, there are more permutations for the 10 choose 5 than there are for the 10 choose 2, if you want those types of scenarios to happen with equal probability, you would set the sum of the probabilities for the 10 choose 2 scenarios to be equal to that of the 10 choose 5.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.