Question: Step 2: Assigning data points to closest centroids Implement the following function in kmeans.py: def assign_data(data_point, centroids): Assign a single data point to the closest

Step 2: Assigning data points to closest centroids

Implement the following function in kmeans.py:

def assign_data(data_point, centroids): """Assign a single data point to the closest centroid. You should use the euclidean_distance function (that you previously implemented). Arguments: data_point: a list of floats representing a data point centroids: a dictionary representing the centroids where the keys are strings (centroid names) and the values are lists of centroid locations Returns: a string as the key name of the closest centroid to the data point """ 

Step 2: Assigning data points to closest centroids Implement the following function

Here is the euclidean_distance function:

def euclidean_distance(dp1, dp2):

distance = 0.0

for i in range(len(dp1)):

distance += (dp1[i]-dp2[i])**2

return math.sqrt(distance)

# problem for students def assign_data(data_point, centroids): ""Assign a single data point to the closest centroid. You should use the euclidean_distance function (that you previously implemented). Arguments: data_point: a list of floats representing a data point centroids: a dictionary representing the centroids where the keys are strings (centroid names) and the values are lists of centroid locations Returns: a string as the key name of the closest centroid to the data point data point = [[1, 4), (3, 7]] centroids {"centl": [1, 1], "cent2":[-1, -1]} return {"centi": [[1, 4], [3, 7]]}

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!