Bernoulli trials using a condition in a vectorized operation

I was wondering how to vectorize the following code instead of using the for-loop and still be able to check for a condition.

# n is a numpy array which keeps the 'number of trials' parameter for the binomial distribution
# p is a numpy array which keeps the 'probability of success' parameter for the binomial distribution
# baf is a numpy array that will keep the results from the np.random.binomial() function

baf = np.repeat(np.nan, n.size)
for index, value in enumerate(n):
if value >= 30:
baf[index] = np.random.binomial(value, p[index]) / value

My own vectorized solution is:

baf = np.repeat(np.nan, n.size)
indices = np.where(n >= 30)
baf[indices] = np.random.binomial(n[indices], p[indices]).astype(float) / n[indices]

However, I was wondering whether there are other more efficient solutions?

• .astype(float) can be dropped if division is true. (That is, by default in Python 3, or in Python 2 if you have from __future__ import division.) – Gareth Rees Sep 25 '14 at 16:26