What is the most efficient way to remove negative elements in an array? I have tried numpy.delete
and Remove all specific value from array and code of the form x[x != i]
.
删除数组中负面元素的最有效方法是什么?我尝试过numpy.delete并从x [x!= i]格式的数组和代码中删除所有特定值。
For:
对于:
import numpy as np
x = np.array([-2, -1.4, -1.1, 0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2])
I want to end up with an array:
我想最终得到一个数组:
[0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2]
4 个解决方案
#1
22
In [2]: x[x >= 0]
Out[2]: array([ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2])
#2
4
There's probably a cool way to do this is numpy because numpy is magic to me, but:
这可能是一个很酷的方法,因为numpy对我来说是神奇的,但是:
x = np.array( [ num for num in x if num >= 0 ] )
#3
4
If performance is important, you could take advantage of the fact that your np.array
is sorted and use numpy.searchsorted
如果性能很重要,您可以利用np.array的排序并使用numpy.searchsorted
For example:
例如:
In [8]: x[np.searchsorted(x, 0) :]
Out[8]: array([ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2])
In [9]: %timeit x[np.searchsorted(x, 0) :]
1000000 loops, best of 3: 1.47 us per loop
In [10]: %timeit x[x >= 0]
100000 loops, best of 3: 4.5 us per loop
The difference in performance will increase as the size of the array increases because np.searchsorted
does a binary search that is O(log n) vs. O(n) linear search that x >= 0
is doing.
随着数组大小的增加,性能的差异将会增加,因为np.searchsorted执行二进制搜索,即O(log n)与O(n)线性搜索x> = 0正在进行。
In [11]: x = np.arange(-1000, 1000)
In [12]: %timeit x[np.searchsorted(x, 0) :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
#4
2
In numpy:
在numpy:
b = array[array>=0]
Example:
例:
>>> import numpy as np
>>> arr = np.array([-2, -1.4, -1.1, 0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2])
>>> arr = arr[arr>=0]
>>> arr
array([ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2])
#1
22
In [2]: x[x >= 0]
Out[2]: array([ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2])
#2
4
There's probably a cool way to do this is numpy because numpy is magic to me, but:
这可能是一个很酷的方法,因为numpy对我来说是神奇的,但是:
x = np.array( [ num for num in x if num >= 0 ] )
#3
4
If performance is important, you could take advantage of the fact that your np.array
is sorted and use numpy.searchsorted
如果性能很重要,您可以利用np.array的排序并使用numpy.searchsorted
For example:
例如:
In [8]: x[np.searchsorted(x, 0) :]
Out[8]: array([ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2])
In [9]: %timeit x[np.searchsorted(x, 0) :]
1000000 loops, best of 3: 1.47 us per loop
In [10]: %timeit x[x >= 0]
100000 loops, best of 3: 4.5 us per loop
The difference in performance will increase as the size of the array increases because np.searchsorted
does a binary search that is O(log n) vs. O(n) linear search that x >= 0
is doing.
随着数组大小的增加,性能的差异将会增加,因为np.searchsorted执行二进制搜索,即O(log n)与O(n)线性搜索x> = 0正在进行。
In [11]: x = np.arange(-1000, 1000)
In [12]: %timeit x[np.searchsorted(x, 0) :]
1000000 loops, best of 3: 1.61 us per loop
In [13]: %timeit x[x >= 0]
100000 loops, best of 3: 9.87 us per loop
#4
2
In numpy:
在numpy:
b = array[array>=0]
Example:
例:
>>> import numpy as np
>>> arr = np.array([-2, -1.4, -1.1, 0, 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10, 14, 16.2])
>>> arr = arr[arr>=0]
>>> arr
array([ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2])