我有一个大的numpy数组,有很多ID值(称之为X):
X:
id rating
1 88
2 99
3 77
4 66
...
Run Code Online (Sandbox Code Playgroud)
我还有另一个numpy"坏ID"数组 - 它表示我想从X中删除的行.
B: [2, 3]
Run Code Online (Sandbox Code Playgroud)
所以,当我完成后,我想:
X:
id rating
1 88
4 66
Run Code Online (Sandbox Code Playgroud)
没有迭代,最简洁的方法是什么?
这是我能想到的最快的方法:
import numpy
x = numpy.arange(1000000, dtype=numpy.int32).reshape((-1,2))
bad = numpy.arange(0, 1000000, 2000, dtype=numpy.int32)
print x.shape
print bad.shape
cleared = numpy.delete(x, numpy.where(numpy.in1d(x[:,0], bad)), 0)
print cleared.shape
Run Code Online (Sandbox Code Playgroud)
这打印:
(500000, 2)
(500,)
(499500, 2)
Run Code Online (Sandbox Code Playgroud)
并且运行速度比ufunc快得多.它将使用一些额外的内存,但是否适合你取决于你的阵列有多大.
说明:
x
的含True如果元素是在bad阵列中,和
False其它.True/ False阵列到含有索引值,其中所述阵列是一个整数数组True.| 归档时间: |
|
| 查看次数: |
4225 次 |
| 最近记录: |