Question: Step 2: Assigning data points to closest centroids Implement the following function in kmeans.py: Here is the euclidean_distance function: Here is the test function: #

Step 2: Assigning data points to closest centroids

Implement the following function in kmeans.py:

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

Here is the euclidean_distance function:

in kmeans.py: Here is the euclidean_distance function: Here is the test function:

Here is the test function:

# problem for students def assign_data(data_point, centroids): ""Assign a single data point

# 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 III # problem for students def euclidean_distance (dp1, dp2): Calculate the Euclidean distance between two data points. Arguments: dp1: a non-empty list of floats representing a data point dp2: a non-empty list of floats representing a data point Returns: the Euclidean distance between two data points distance = 0.0 for i in range(len(dpi)): distance += (dp1[i]-dp2[i]) **2 return math.sqrt(distance) def test_assign_data(): "'"'Function for verifying if assign_data is correctly implemented. Will throw an error if it isn't. # You do not need to change anything in this function # ## # set up data_empty = [0, 0, 0, 0] data_random [1.1, 5.3, 55, -12.1] centroids = {"centroidi": [1, 1, 1, 1], "centroid2": [-10.1, 1, 23.2, 5.099]} assert assign_data(data_empty, centroids) "centroidl" assert assign_data(data_random, centroids) "centroid2" data = (10.1, 1, 23.2, 5.099] centroids = {"centroidl": [1, 1, 1, 1], "centroid2": [10, 1, 23, 5), "centroid3": [-100, 20.2, 52.9, -37.088]} assert assign_data(data, centroids) "centroid2" print("test_assign_data passed.")

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!