Question: I have a problem with python programming. I cant passed the 3 Part test: Problem 3 : Update assignment of points to centroids Implement assign
I have a problem with python programming. I cant passed the Part test:
Problem : Update assignment of points to centroids
Implement assignpointstocentroids to assign data points to their closest centroid:
def assignpointstocentroidslistofpoints centroidsdict: Assign all data points to the closest centroids. You should use the getclosestcentroid function that you previously implemented This function should return a new dictionary, not modify any passed in parameters. Arguments: listofpoints: a list of lists representing all data points centroidsdict: a dictionary representing the centroids where the keys are strings centroid names and the values are lists of centroid locations Returns: a new dictionary whose keys are the centroids' key names and values are lists of points that belong to the centroid. If a given centroid does not have any data points closest to it do not include the centroid in the returned dictionary Example: Code: listofpoints centroidsdict centroid: "centroid: printassignpointstocentroidslistofpoints centroidsdict Output: centroid: 'centroid:
If there are no data points assigned to a centroid, then you should not include that centroid in the returned dictionary.
You should see the following output from running testkmeanspy once you have correctly implemented assignpointstocentroids:
"testassignpointstocentroids passed."
Here's my code for this part. it's a function but I don't know which algorithm I did wrong here.
def assignpointstocentroidslistofpoints centroidsdict:
assignmentdict
for point in listofpoints:
closestcentroid getclosestcentroidpoint centroidsdict
if closestcentroid not in assignmentdict:
assignmentdictclosestcentroid
assignmentdictclosestcentroidappendpoint
return assignmentdict
Here's the test code for testfunction:
def testassignpointstocentroids:
Function for verifying if assignupdate is correctly implemented.
Will throw an error if it isn't.
#######################################################
# You do not need to change anything in this function #
#######################################################
try:
r assignpointstocentroidscentroid:
if r is None:
systracebacklimit
raise Exception
"assignpointstocentroids returned None; have you implemented it yet?"
finally:
pass
# set up
listofpoints centroidsdict centroidsdict setupdatacentroids
# centroidsdict
received assignpointstocentroidslistofpoints centroidsdict
expected
"centroid:
"centroid:
assertequalsexpected received
# centroidsdict
received assignpointstocentroidslistofpoints centroidsdict
expected
"centroid:
>
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
