Python/Python 기타
한 줄로 Euclidean distance matrix 만들기 (Python)
거위쪼아
2021. 9. 27. 16:29
한 줄로 Euclidean distance matrix 만들기 (Python)
N*2 dimension 의 numpy array 가 있을 때 각 element 별로 간단하게 pairwise euclidean distance matrix 를 생성하고 싶을 때가 있습니다.
이때 다음과 같이 쉽게 구할 수 있습니다.
from sklearn.metrics.pairwise import euclidean_distances
a = np.array( [[1,2],
[3,4],
[5,6]])
print(euclidean_distances(a,a))
##out##
[[0. 2.82842712 5.65685425]
[2.82842712 0. 2.82842712]
[5.65685425 2.82842712 0. ]]