def closest_centroid(points, centroids):
"""returns an array containing the index to the nearest centroid for each point"""
distances = np.sqrt(((points - centroids[:, np.newaxis])**2).sum(axis=2))
return np.argmin(distances, axis=0)
이 함수의 정확한 작동 원리를 설명 할 수 있습니까? 그래서이 Python 함수에서 어떤 일이 일어나는지 이해하려고 시도합니다.
31998888119 0.94 34
23423423422 0.45 43
....
그리고 : 저는 현재 보이는 points
을 얻었다. points[2]
가 0.94
이고 points[3]
가 제 항목 34
있을 것이지만이 numpy
어레이에서 points[1]
장기 ID 것이다.
def initialize_centroids(points, k):
"""returns k centroids from the initial points"""
centroids = points.copy()
np.random.shuffle(centroids)
return centroids[:k]
지금 내가 처음 ID의 열 및 centroids
(다시 한 번 첫 번째 열을 무시 무시 points
의 값에서 유클리드 거리를 얻으려면 :
무게 중심이 특정 배열에서 그냥 무작위 선택이다). 정확히 distances = np.sqrt(((points - centroids[:, np.newaxis])**2).sum(axis=2))
줄의 구문을 이해하지 못합니다. 새 축에 대한 해독이있는 동안 정확하게 세 번째 열을 기준으로 합산하는 이유는 무엇입니까? np.newaxis
? 또한 어떤 축을 따라 np.argmin
을 작동시켜야합니까?