从numpy数组中选择"一些"随机点

jdo*_*doe 6 python arrays numpy

我有两个相关的numpy数组,Xy.我需要从中选择n随机行X并将其存储在一个数组中,相应的y值并将随机选择的点的索引追加到它.

我有另一个数组index存储索引列表,我不想采样.

我怎样才能做到这一点?

样本数据:

index = [2,3]
X = np.array([[0.3,0.7],[0.5,0.5] ,[0.2,0.8], [0.1,0.9]])
y = np.array([[0], [1], [0], [1]])
Run Code Online (Sandbox Code Playgroud)

如果这些X是随机选择的(在哪里n=2):

randomylSelected = np.array([[0.3,0.7],[0.5,0.5]])
Run Code Online (Sandbox Code Playgroud)

期望的输出将是:

index = [0,1,2,3]
randomlySelectedY = [0,1]
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

piR*_*red 0

我将管理一个布尔值数组,我不断地使用它来对索引数组进行切片并从结果中随机选择。

n = X.shape[0]
sampled = np.empty(n, dtype=np.bool)
sampled.fill(False)
rng = np.arange(n)

k = 2

while not sampled.all():
    sample = np.random.choice(rng[~sampled], size=k, replace=False)
    print(X[sample])
    print()
    print(y[sample])
    print()
    sampled[sample] = True

[[ 0.2  0.8]
 [ 0.5  0.5]]

[[0]
 [1]]

[[ 0.3  0.7]
 [ 0.1  0.9]]

[[0]
 [1]]
Run Code Online (Sandbox Code Playgroud)