简体   繁体   English

两个数组的Python随机样本,但匹配索引

[英]Python random sample of two arrays, but matching indices

I have two numpy arrays x and y, which have length 10,000.我有两个 numpy 数组 x 和 y,它们的长度为 10,000。 I would like to plot a random subset of 1,000 entries of both x and y.我想绘制 x 和 y 的 1,000 个条目的随机子集。 Is there an easy way to use the lovely, compact random.sample(population, k) on both x and y to select the same corresponding indices?有没有一种简单的方法可以在 x 和 y 上使用可爱、紧凑的 random.sample(population, k) 来选择相同的对应索引? (The y and x vectors are linked by a function y(x) say.) (y 和 x 向量通过函数 y(x) 说。)

Thanks.谢谢。

You can use np.random.choice on an index array and apply it to both arrays:您可以在索引数组上使用np.random.choice并将其应用于两个数组:

idx = np.random.choice(np.arange(len(x)), 1000, replace=False)
x_sample = x[idx]
y_sample = y[idx]

Just zip the two together and use that as the population:只需将两者压缩在一起并将其用作人口:

import random

random.sample(zip(xs,ys), 1000)

The result will be 1000 pairs (2-tuples) of corresponding entries from xs and ys .结果将是来自xsys 1000 对(2 元组)对应条目。

After test numpy.random.choice solution, I found out it was very slow for larger array.在测试numpy.random.choice解决方案后,我发现对于较大的数组它非常慢。

numpy.random.randint should be much faster numpy.random.randint应该快得多

example例子

x = np.arange(1e8)
y = np.arange(1e8)
idx = np.random.randint(0, x.shape[0], 10000)
return x[idx], y[idx]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM