Question: Implement in Python a function label (data, centers) that outputs a la- beling of all the objects in data: label c, which is a number

 Implement in Python a function label (data, centers) that outputs a

Implement in Python a function label (data, centers) that outputs a la- beling of all the objects in data: label c, which is a number between 0 and len(centers)-1, is assigned to x if and only if centers [c] is the nearest cen- ter to the midpoint between x and its nearest neighbour in data. A simple algorithm is the following - labels = empty list - for all x in data - nn = the nearest neighbour of x in data - mid = the midpoint between x and nn - cent_dist = distances between mid and all elements of centers - find the smallest distance and its index in cent_dist - append the (x, index) to labels - return labels To compute the nearest neighbour of x use the following Python functions def sq_sum(x): return sum((y ** 2 for y in x)) def sq_norm_diff(x, y): return sq-sum((z[0] - z[1] for z in zip(x, y))) def nn(data, x): distances = list(enumerate([sq_norm_diff(x, y) for y in data])) m - min (distances, key = lambda t: t[1]) del distances[m[0]] m = min (distances, key = lambda t: t[1]) return data[m[0]]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!