Question: Can I please get an explanation about this test code? deftest_assign_data(): #setup data_empty=[0,0,0,0] data_random=[1.1,5.3,55,-12.1] centroids={centroid1:[1,1,1,1], centroid2:[-10.1,1,23.2,5.099]} assertassign_data(data_empty,centroids)==centroid1 assertassign_data(data_random,centroids)==centroid2 data=[10.1,1,23.2,5.099] centroids={centroid1:[1,1,1,1], centroid2:[10,1,23,5], centroid3:[-100,20.2,52.9,-37.088]} assertassign_data(data,centroids)==centroid2 print(test_assign_datapassed.) The

Can I please get an explanation about this test code?

deftest_assign_data():

#setup

data_empty=[0,0,0,0]

data_random=[1.1,5.3,55,-12.1]

centroids={"centroid1":[1,1,1,1],

"centroid2":[-10.1,1,23.2,5.099]}

assertassign_data(data_empty,centroids)=="centroid1"

assertassign_data(data_random,centroids)=="centroid2"

data=[10.1,1,23.2,5.099]

centroids={"centroid1":[1,1,1,1],

"centroid2":[10,1,23,5],

"centroid3":[-100,20.2,52.9,-37.088]}

assertassign_data(data,centroids)=="centroid2"

print("test_assign_datapassed.")

The following is my code

defassign_data(data_point,centroids):

"""Assignasingledatapointtotheclosestcentroid.Youshoulduse

theeuclidean_distancefunction(thatyoupreviouslyimplemented).

Arguments:

data_point:alistoffloatsrepresentingadatapoint

centroids:adictionaryrepresentingthecentroidswherethekeysare

strings(centroidnames)andthevaluesarelistsof

centroidlocations

Returns:astringasthekeynameoftheclosestcentroidtothedatapoint

"""

min=float()

centroid=""

fork,vincentroid.items():

euc_dist=euclidean_distance(data_point,v)

ifmin>euc_dist:

min=euc_dist

centroid=k

returncentroid

Im getting this error

assert assign_data(data_empty, centroids) == "centroid1"

for k, v in centroid.items():

AttributeError: 'str' object has no attribute 'items'

I am confused about what im doing wrong, please help!!

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 Programming Questions!