목록 함축은 매우 비효율적 방법 numpy 배열을 처리합니다. 거리 계산에 특히 적합하지 않습니다.
데이터와 포인트의 차이점을 찾으려면 data - point
을 사용해보세요. 그런 다음 np.hypot
을 사용하여 거리를 계산하거나 원하는 경우 사각형을 합계하여 제곱근을 취합니다.
계산을 위해 Nx2 배열을 만들면 조금 더 쉽습니다.
import numpy as np
data = np.array([[[1704, 1240],
[1745, 1244],
[1972, 1290],
[2129, 1395],
[1989, 1332]],
[[1712, 1246],
[1750, 1246],
[1964, 1286],
[2138, 1399],
[1989, 1333]],
[[1721, 1249],
[1756, 1249],
[1955, 1283],
[2145, 1399],
[1990, 1333]]])
point = [1989, 1332]
#-- Calculate distance ------------
# The reshape is to make it a single, Nx2 array to make calling `hypot` easier
dist = data.reshape((-1,2)) - point
dist = np.hypot(*dist.T)
# We can then reshape it back to AxBx1 array, similar to the original shape
dist = dist.reshape(data.shape[0], data.shape[1], 1)
print dist
이 수율 :
array([[[ 299.48121811],
[ 259.38388539],
[ 45.31004304],
[ 153.5219854 ],
[ 0. ]],
[[ 290.04310025],
[ 254.0019685 ],
[ 52.35456045],
[ 163.37074401],
[ 1. ]],
[[ 280.55837182],
[ 247.34186868],
[ 59.6405902 ],
[ 169.77926846],
[ 1.41421356]]])
지금, 가장 가까운 요소를 제거하는 것은 단순히 가장 가까운 요소를 얻기보다 약간 어렵
기본적으로, 당신은 이런 식으로 뭔가를 할 수 있습니다.
numpy를 사용하면 부울 인덱스를 사용하여이를 쉽게 처리 할 수 있습니다.
그러나 축 정렬에 대해 조금 걱정할 필요가 있습니다.
키는 축 번호가 인 축 ( 축)을 따라 "브로드 캐스팅"하는 것을 이해하는 것이 중요합니다. 이 경우 중간 축을 따라 브로드 캐스트를 원합니다.
또한 -1
은 축 크기의 자리 표시 자로 사용할 수 있습니다. Numpy는 축 크기로 -1
을 입력하면 허용되는 크기를 계산합니다.
이
#-- Remove closest point ---------------------
mask = np.squeeze(dist) != dist.min(axis=1)
filtered = data[mask]
# Once again, let's reshape things back to the original shape...
filtered = filtered.reshape(data.shape[0], -1, data.shape[2])
당신은 한 줄, 난 그냥 가독성을 위해 그것을 분해하고있어 것을 만들 수 : 우리가 조금 같을 것이다해야 할 것입니다 무엇
. 중요한 점은 dist != something
이 부울 배열을 생성 한 다음 원래 배열을 인덱싱하는 데 사용할 수 있다는 것입니다.
그래서, 모두 함께 퍼팅 :
import numpy as np
data = np.array([[[1704, 1240],
[1745, 1244],
[1972, 1290],
[2129, 1395],
[1989, 1332]],
[[1712, 1246],
[1750, 1246],
[1964, 1286],
[2138, 1399],
[1989, 1333]],
[[1721, 1249],
[1756, 1249],
[1955, 1283],
[2145, 1399],
[1990, 1333]]])
point = [1989, 1332]
#-- Calculate distance ------------
# The reshape is to make it a single, Nx2 array to make calling `hypot` easier
dist = data.reshape((-1,2)) - point
dist = np.hypot(*dist.T)
# We can then reshape it back to AxBx1 array, similar to the original shape
dist = dist.reshape(data.shape[0], data.shape[1], 1)
#-- Remove closest point ---------------------
mask = np.squeeze(dist) != dist.min(axis=1)
filtered = data[mask]
# Once again, let's reshape things back to the original shape...
filtered = filtered.reshape(data.shape[0], -1, data.shape[2])
print filtered
수익률 : 두 개 이상의 지점이 똑같이 가까운 경우
array([[[1704, 1240],
[1745, 1244],
[1972, 1290],
[2129, 1395]],
[[1712, 1246],
[1750, 1246],
[1964, 1286],
[2138, 1399]],
[[1721, 1249],
[1756, 1249],
[1955, 1283],
[2145, 1399]]])
를 보조 노트에,이 작동하지 않습니다. Numpy 배열은 각 차원에서 동일한 수의 요소를 가져야하므로이 경우 그룹화를 다시해야합니다.
아, 게시하기 전에 어떻게 든 보지 못했습니다. 나는'apply_along_axis'를 사용하는 것을 생각했지만, 테스트 해봤는데 훨씬 빠릅니다. – senderle
'apply_along_axis'는 메모리를 적게 사용해야하므로 두 방법 모두 여전히 유용합니다! –
감사! 매우 간결하면서도 유익합니다. 너무 빨리. – OneTrickyPony