kil*_*les 8 python arrays numpy
说我有一些长数组和一系列指数.如何选择除那些指数以外的所有内容?我找到了一个解决方案,但它并不优雅:
import numpy as np
x = np.array([0,10,20,30,40,50,60])
exclude = [1, 3, 5]
print x[list(set(range(len(x))) - set(exclude))]
Run Code Online (Sandbox Code Playgroud)
use*_*ica 13
这是做什么的numpy.delete.(它不会修改输入数组,因此您不必担心.)
In [4]: np.delete(x, exclude)
Out[4]: array([ 0, 20, 40, 60])
Run Code Online (Sandbox Code Playgroud)
hpa*_*ulj 13
np.delete 做各种各样的事情取决于你给它,但在这种情况下,它使用如下的面具:
In [604]: mask = np.ones(x.shape, bool)
In [605]: mask[exclude] = False
In [606]: mask
Out[606]: array([ True, False, True, False, True, False, True], dtype=bool)
In [607]: x[mask]
Out[607]: array([ 0, 20, 40, 60])
Run Code Online (Sandbox Code Playgroud)
np.in1d或np.isin基于创建布尔索引exclude可能是一种替代方法:
x[~np.isin(np.arange(len(x)), exclude)]
# array([ 0, 20, 40, 60])
x[~np.in1d(np.arange(len(x)), exclude)]
# array([ 0, 20, 40, 60])
Run Code Online (Sandbox Code Playgroud)