193k views
4 votes
create a function centroidassignment(l,c) that takes two inputs. the input l is the same as in step 1, and c will be a set of points that represent the centroids from step 2. the function d, given below, is part of the template and should be used to calculate the square of the distance between two points. def d(p1,p2): return (p1[0]-p2[0])**2 (p1[1]-p2[1])**2 for each point in l, determine which of the k centroids the point is closest to. if there are two or more centroids that yield the same minimum distance, assign the point to the centroid listed earliest in c. the function should return a list a of n numbers 0 through k-1 where a[i]

User Dhaupin
by
7.5k points

1 Answer

3 votes

Answer:

Here is the code:

def centroidassignment(l, c):

def d(p1, p2):

return (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2

assignments = []

for point in l:

min_distance = float('inf')

centroid_index = None

for i, centroid in enumerate(c):

distance = d(point, centroid)

if distance < min_distance:

min_distance = distance

centroid_index = i

assignments.append(centroid_index)

return assignments

Step-by-step explanation:

User NStuke
by
8.3k points