Skip to content Skip to sidebar Skip to footer

Choose Elements From Two Numpy Arrays At Random

I am trying to merge two numpy arrays together by choosing elements from each at random. Say I have two arrays of equal length x and y as follows: x = np.arange(10) y = np.arange(

Solution 1:

How about this?

z = y.copy()
z[r] = x[r]

Solution 2:

This is a one-liner using the np.where(<condition>, <where_true>, <where_false>) syntax of np.where:

z = np.where(r, x, y)

Post a Comment for "Choose Elements From Two Numpy Arrays At Random"