2017-12-11 4 views
3

에서의 거리에 따라 좌표 나는 두 배열이 말하는이 :필터링이 점

A = np.array([[ 1. , 1. , 0.5 ], 
       [ 2. , 2. , 0.7 ], 
       [ 3. , 4. , 1.2 ], 
       [ 4. , 3. , 2.33], 
       [ 1. , 2. , 0.5 ], 
       [ 6. , 5. , 0.3 ], 
       [ 4. , 5. , 1.2 ], 
       [ 5. , 5. , 1.5 ]]) 

B = np.array([2,1]) 

내가 B 2의 반경 내에 있지 A의 모든 값을 찾으려는 것이다.

내 대답은해야한다 :이 작업을 수행 할 수있는 파이썬 방법은

C = [[3,4,1.2],[4,3,2.33],[6,5,0.3],[4,5,1.2],[5,5,1.5]] 

있습니까?

radius = 2 
C.append(np.extract((cdist(A[:, :2], B[np.newaxis]) > radius), A)) 

하지만 np.extractA을 평평하게하고 내가이 예상되는 것을 얻을 해달라고 실현 : 내가 시도 무엇

이다.

답변

3

여기서 R을 반경으로 설정하십시오. 우리는 다음과 같이 그것을 해결할 방법이 거의 없을 것입니다.

- np.linalg.norm 사용 :

d = A[:,:2] - B 
out = A[np.einsum('ij,ij->i', d,d) > R**2] 

접근법 # 3 - np.einsum 사용 - :

접근법 1

from scipy.spatial.distance import cdist 

A[(cdist(A[:,:2],B[None]) > R).ravel()] 

접근법 # 2 cdist 사용 73,210

접근법 # 4 :matrix-multiplicationnp.dot 함께 사용 -

A[(A[:,:2]**2).sum(1) + (B**2).sum() - 2*A[:,:2].dot(B) > R**2] 

접근법 # 5 : - 사용

A[np.einsum('ij,ij->i',A[:,:2],A[:,:2]) + B.dot(B) - 2*A[:,:2].dot(B) > R**2] 

접근법 # 6 einsum의 조합 matrix-multiplication 사용broadcasting -

따라서
A[((A[:,:2] - B)**2).sum(1) > R**2] 

, 단순히 위에서 언급 한 솔루션 <> 교체 반경 R 내 포인트를 얻을 수 있습니다.

2

@Divakar 언급하지 또 다른 유용한 방법은 사용되는 cKDTree :

from scipy.spatial import cKDTree 

# Find indices of points within radius 
radius = 2 
indices = cKDTree(A[:, :2]).query_ball_point(B, radius) 

# Construct a mask over these points 
mask = np.zeros(len(A), dtype=bool) 
mask[indices] = True 

# Extract values not among the nearest neighbors 
A[~mask] 

주된 이점은, 어레이 크기가 증가 직접적인 방법보다 훨씬 빠를 것 인 데이터 때문에 구조는 A의 모든 점에 대한 거리 계산을 피합니다.