Numpy 从 2 个数组中选择元素
Numpy Choose Elements from 2 arrays
我需要从 2 个数组中选择 n 个项目,以使索引相同。因此,例如,我需要从 x 中随机选择两个项目,并从 y 中选择元素,使得 y 的选择索引与 x 的相同:
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
x_chosen = np.random.choice(x,(2,))
说 x_chosen
结果是:x_chosen = [0.1123,0.223]...
那么我需要:
y_chosen = [1,1]
我目前有一个解决方法...但我想要一个基本的 numpy 或 scipy 函数而不是我自己的基本上只有 3 行的函数,以使我在这个项目中的函数保持在范围内.. .我宁愿不要在我的主要代码中创建这个一次性变量:
x_index = np.asarray([i for i in range(len(x))])
x_chosen_indices = np.random.choice(x_index,(2,))
x_chosen = x[x_chosen_indices]
y_chosen = y[x_chosen_indices]
或者,我可以堆叠、选择和拆分...但我想这仍然给我留下了一个一次性函数,我必须坚持在某个地方...或 4-5 行没有意义的代码。 ..
您可以使用 numpy.random.permutation
创建一个随机排列的索引数组。然后,您可以 select 随机 select 任意数量的公共元素:
import numpy as np
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices= np.random.permutation(np.size(x, 0))
x_chosen = x[indices[:2]]
y_chosen = y[indices[:2]]
print(x_chosen)
>>>[ 0.8873 0.1123]
print(y_chosen)
>>>[2 1]
首先选择指数如何:
import numpy as np
choice = np.random.choice(np.array(range(len(x))), 2)
然后根据他们选择:
x_chosen, y_chosen = x[choice], y[choice]
以下是我认为可能有效的两种方法。第一个与上面的方法有点相同,但它确实去掉了一行:
index = np.random.choice(np.arange(len(x)), 2, replace=False)
x_chosen = x[index]
y_chosen = y[index]
In [15]: x_chosen
Out[15]: array([ 0.8873, 0.223 ])
我不确定这是否是您想要的,但它是一个单行代码,可以为您提供 (x, y) 的元组:
import random
random.sample(zip(x, y), 2)
Out[22]: [(0.223, 1), (0.1123, 1)]
使用 np.random.randint()
查找您的指数:
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices = np.random.randint(0, x.size, 2)
>>> x[indices]
array([ 0.1123, 0.223 ])
>>> y[indices]
array([1, 1])
编辑
正如 B.M 在评论中提到的,使用 np.random.choice(.., replace=False)
来
避免多次使用相同的索引:
indices = np.random.choice(x.size, 2, replace=False)
我需要从 2 个数组中选择 n 个项目,以使索引相同。因此,例如,我需要从 x 中随机选择两个项目,并从 y 中选择元素,使得 y 的选择索引与 x 的相同:
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
x_chosen = np.random.choice(x,(2,))
说 x_chosen
结果是:x_chosen = [0.1123,0.223]...
那么我需要:
y_chosen = [1,1]
我目前有一个解决方法...但我想要一个基本的 numpy 或 scipy 函数而不是我自己的基本上只有 3 行的函数,以使我在这个项目中的函数保持在范围内.. .我宁愿不要在我的主要代码中创建这个一次性变量:
x_index = np.asarray([i for i in range(len(x))])
x_chosen_indices = np.random.choice(x_index,(2,))
x_chosen = x[x_chosen_indices]
y_chosen = y[x_chosen_indices]
或者,我可以堆叠、选择和拆分...但我想这仍然给我留下了一个一次性函数,我必须坚持在某个地方...或 4-5 行没有意义的代码。 ..
您可以使用 numpy.random.permutation
创建一个随机排列的索引数组。然后,您可以 select 随机 select 任意数量的公共元素:
import numpy as np
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices= np.random.permutation(np.size(x, 0))
x_chosen = x[indices[:2]]
y_chosen = y[indices[:2]]
print(x_chosen)
>>>[ 0.8873 0.1123]
print(y_chosen)
>>>[2 1]
首先选择指数如何:
import numpy as np
choice = np.random.choice(np.array(range(len(x))), 2)
然后根据他们选择:
x_chosen, y_chosen = x[choice], y[choice]
以下是我认为可能有效的两种方法。第一个与上面的方法有点相同,但它确实去掉了一行:
index = np.random.choice(np.arange(len(x)), 2, replace=False)
x_chosen = x[index]
y_chosen = y[index]
In [15]: x_chosen
Out[15]: array([ 0.8873, 0.223 ])
我不确定这是否是您想要的,但它是一个单行代码,可以为您提供 (x, y) 的元组:
import random
random.sample(zip(x, y), 2)
Out[22]: [(0.223, 1), (0.1123, 1)]
使用 np.random.randint()
查找您的指数:
x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])
indices = np.random.randint(0, x.size, 2)
>>> x[indices]
array([ 0.1123, 0.223 ])
>>> y[indices]
array([1, 1])
编辑
正如 B.M 在评论中提到的,使用 np.random.choice(.., replace=False)
来
避免多次使用相同的索引:
indices = np.random.choice(x.size, 2, replace=False)