选择一些'来自numpy数组的随机点

时间:2017-04-19 23:24:29

标签: 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]])

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

randomylSelected = np.array([[0.3,0.7],[0.5,0.5]])

所需的输出是:

index = [0,1,2,3]
randomlySelectedY = [0,1]

我该怎么做?

2 个答案:

答案 0 :(得分: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]]

答案 1 :(得分:0)

如果您想随机选择n行,则选择任意行的概率相等:

n = 2 #for sake of argument
randomlySelectedY = np.argsort(np.random.random(4))[:n] #generate a 1x4 array of random, uniformly distributed numbers and then select the indices of the lowest n numbers

randomylSelected = X[randomlySelectedY]
index = np.linspace(1,np.size(X[:,1]),np.size(X[:,1]))