Generating a random numpy ndarray of 0 and 1 with a specific range of 1 values
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
np.random.shuffle(my_array)
Can you please help me in finding a better solution?
Topic numpy probability programming python statistics
Category Data Science