从两个numpy数组中随机选择元素
问题描述:
我试图通过从每个元素中随机选择元素来将两个numpy数组合并在一起.
假设我有两个长度分别为x
和y
的数组,如下所示:
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(10, 20)
和遮罩r
:
r = np.random.choice([True, False], 10)
那么有什么方法可以从r
是True
的x
和r
是False
的y
中选择元素?
Then is there any way to select elements from x
where r
is True
and from y
where r
is False
?
我不必使用遮罩方法,但是我需要一些快速的东西,因为x
和y
实际上要比10长得多,因此理想情况下不涉及循环.
I don't have to use the mask approach, but I need something fast as x
and y
will be much longer than 10 in reality so ideally no looping involved.
答
如何?
z = y.copy()
z[r] = x[r]