简体   繁体   English

从 numpy 数组中随机选择

[英]Randomly select from numpy array

I have two related numpy arrays, X and y .我有两个相关的 numpy 数组Xy I need to select n random rows from X and store this in an array, the corresponding y value and the appends to it the index of the points randomly selected.我需要从X中选择n随机行并将其存储在一个数组中,对应的y值和附加到它的随机选择的点的索引。

I have another array index which stores a list of index which I dont want to sample.我有另一个数组index ,它存储我不想采样的索引列表。

How can I do this?我怎样才能做到这一点?

Sample data:样本数据:

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]])

If these X 's were randomly selected (where n=2 ):如果这些X是随机选择的(其中n=2 ):

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

the desired output would be:所需的输出是:

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

How can I do this?我怎样才能做到这一点?

You can create random indices with np.random.choice :您可以使用np.random.choice创建随机索引:

n = 2  # for 2 random indices
index = np.random.choice(X.shape[0], n, replace=False)  

Then you just need to index your arrays with the result:然后你只需要用结果索引你的数组:

x_random = X[index]
y_random = Y[index]

just to wrap @MSeifert 's answer in a function:只是将@MSeifert 的答案包装在一个函数中:

def random_sample(arr: numpy.array, size: int = 1) -> numpy.array:
    return arr[np.random.choice(len(arr), size=size, replace=False)]

useage:用途:

randomly_selected_y = random_sample(Y)

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

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