[python] numpy를 사용하는 배열의 효율적인 임계 값 필터

특정 임계 값보다 낮은 요소를 제거하려면 배열을 필터링해야합니다. 내 현재 코드는 다음과 같습니다.

threshold = 5
a = numpy.array(range(10)) # testing data
b = numpy.array(filter(lambda x: x >= threshold, a))

문제는 람다 함수가있는 필터를 사용하여 임시 목록을 생성한다는 것입니다 (느림).

이것은 매우 간단한 작업이므로 효율적인 방식으로 수행하는 numpy 함수가있을 수 있지만 찾을 수 없었습니다.

이를 달성하는 또 다른 방법은 배열을 정렬하고 임계 값의 인덱스를 찾고 해당 인덱스에서 슬라이스를 반환하는 것이 될 수 있다고 생각했습니다.하지만 이것이 작은 입력에 대해 더 빠르더라도 ), 입력 크기가 증가함에 따라 점근 적으로 덜 효율적입니다.

어떤 아이디어? 감사!

업데이트 : 나도 몇 가지 측정을 수행했으며 입력이 100.000.000 항목 일 때 정렬 + 슬라이싱이 순수한 파이썬 필터보다 두 배 빠릅니다.

In [321]: r = numpy.random.uniform(0, 1, 100000000)

In [322]: %timeit test1(r) # filter
1 loops, best of 3: 21.3 s per loop

In [323]: %timeit test2(r) # sort and slice
1 loops, best of 3: 11.1 s per loop

In [324]: %timeit test3(r) # boolean indexing
1 loops, best of 3: 1.26 s per loop



답변

b = a[a>threshold] 이것은해야한다

다음과 같이 테스트했습니다.

import numpy as np, datetime
# array of zeros and ones interleaved
lrg = np.arange(2).reshape((2,-1)).repeat(1000000,-1).flatten()

t0 = datetime.datetime.now()
flt = lrg[lrg==0]
print datetime.datetime.now() - t0

t0 = datetime.datetime.now()
flt = np.array(filter(lambda x:x==0, lrg))
print datetime.datetime.now() - t0

나는 얻었다

$ python test.py
0:00:00.028000
0:00:02.461000

http://docs.scipy.org/doc/numpy/user/basics.indexing.html#boolean-or-mask-index-arrays


답변