我有两个相关的numpy数组,X
和y
。我需要从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]
我该怎么做?
答案 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]))