python - Using np.random.randint as fill_value -
i want create numpy
array, each element amount of 1
s in numpy
array of size x
created np.random.randint
.
>>> x = 10 >>> np.random.randint(2, size=x) array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1]) >>> sum(array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1])) 5
and using results in same array being used, instead of generating new random 1 each time
>>> np.full((5,), sum(np.random.randint(2, size=10)), dtype="int") array([5, 5, 5, 5, 5])
how can this, or there better way this? tried following
>>> = np.random.rand(10) >>> len(a[a < 0.5]) 7 >>> np.full((5,), len(np.random.rand(10)[np.random.rand(10) < 0.5]), dtype="int") array([7, 7, 7, 7, 7])
but can see resulted in same numbers. problem don't want use loops, , instead find way using numpy
.
you generate matrix n
arrays each of size x
made of random ints. sum on each array,
import numpy np x = 10 n = 5 = np.sum(np.random.randint(2, size=[n,x]),0)
i'm sure np.full
not want here array initialisation single value.
Comments
Post a Comment