Numpy切片:除这些条目外的所有条目

Numpy切片:除这些条目外的所有条目,numpy,Numpy,我有一个numpy数组a。我想选择这个数组的随机样本作为交叉验证的测试和训练集。作为一个训练集,我通过选择条目idx来使用切片。有没有办法选择对这些条目的赞美?i、 e.不在idx中的所有条目 # N: size of numpy array a. idx = random.sample(np.arange(N),N/10) # select random sample train(a[idx]) # train on this random sample test(a[ NOT idx]) #

我有一个numpy数组a。我想选择这个数组的随机样本作为交叉验证的测试和训练集。作为一个训练集,我通过选择条目idx来使用切片。有没有办法选择对这些条目的赞美?i、 e.不在idx中的所有条目

# N: size of numpy array a.
idx = random.sample(np.arange(N),N/10) # select random sample
train(a[idx]) # train on this random sample
test(a[ NOT idx]) # test on the rest. 
如何以紧凑的方式调用最后一行的其余条目?
谢谢。

如果将
idx
设置为布尔数组,则可以使用
~idx
选择补码:

import numpy as np

N = len(a)
idx = np.zeros(N, dtype='bool')
idx[np.random.choice(np.arange(N), size=N/10, replace=False)] = True
train(a[idx]) # train on this random sample
test(a[~idx]) # test on the rest.
测试(一个[np.setdiff1d(np.arange(N),idx)])
也起作用