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 3 Part test:
Problem 3: Update assignment of points to centroids
Implement assign_(p)oints_(t)o_(c)entroids() to assign data points to their closest centroid:
def assign_(p)oints_(t)o_(c)entroids(list_(o)f_(p)oints, centroids_(d)ict): """ Assign all data points to the closest centroids. You should use the get_(c)losest_(c)entroid function (that you previously implemented). This function should return a new dictionary, not modify any passed in parameters. Arguments: list_(o)f_(p)oints: a list of lists representing all data points centroids_(d)ict: 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: list_(o)f_(p)oints =[[1.1,1,1,0.5],[4,3.14,2,1],[0,0,0,0]] centroids_(d)ict ={"centroid1": [1,1,1,1], "centroid2": [2,2,2,2]} print(assign_(p)oints_(t)o_(c)entroids(list_(o)f_(p)oints, centroids_(d)ict)) Output: {'centroid1': [[1.1,1,1,0.5],[0,0,0,0]], 'centroid2': [[4,3.14,2,1]]}"""
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 test_(k)means.py once you have correctly implemented assign_(p)oints_(t)o_(c)entroids():
"test_(a)ssign_(p)oints_(t)o_(c)entroids passed."
Here's my code for this part. it's a function but I don't know which algorithm I did wrong here.
def assign_(p)oints_(t)o_(c)entroids(list_(o)f_(p)oints, centroids_(d)ict):
assignment_(d)ict ={}
for point in list_(o)f_(p)oints:
closest_(c)entroid = get_(c)losest_(c)entroid(point, centroids_(d)ict)
if closest_(c)entroid not in assignment_(d)ict:
assignment_(d)ict[closest_(c)entroid]=[]
assignment_(d)ict[closest_(c)entroid].append(point)
return assignment_(d)ict
Here's the test code for test_(f)unction:
def test_(a)ssign_(p)oints_(t)o_(c)entroids():
"""Function for verifying if assign_(u)pdate is correctly implemented.
Will throw an error if it isn't.
"""
#######################################################
# You do not need to change anything in this function #
#######################################################
try:
r = assign_(p)oints_(t)o_(c)entroids([[0]],{"centroid0": [0]})
if r is None:
sys.tracebacklimit =0
raise Exception(
"assign_(p)oints_(t)o_(c)entroids() returned None; have you implemented it yet?")
finally:
pass
# set up
list_(o)f_(p)oints, centroids_(d)ict1, centroids_(d)ict2= setup_(d)ata_(c)entroids()
# centroids_(d)ict1
received = assign_(p)oints_(t)o_(c)entroids(list_(o)f_(p)oints, centroids_(d)ict1)
expected ={
"centroid1": [[-1.36639346,-0.38664658,-1.02232584,-1.05902604],
[1.13659605,-2.47109085,-0.83996912,-0.24579457],
[-0.83095884,-1.73002213,-0.01361636,-0.3265274]],
"centroid2": [[-1.01714716,0.95954521,1.20493919,0.34804443],
[-1.48090019,-1.47491857,-0.6221167,1.79055006],
[-0.31237952,0.73762417,0.39042814,-1.1308523],
[-0.78645408,1.98342914,0.31944446,-0.41656898],
[-1.06190687,0.34481172,-0.70359847,-0.27828666],
[-2.01157677,2.93965872,0.32334723,-0.1659333],
[-0.56669023,-0.06943413,1.46053764,0.01723844]]
}
assert_(e)quals(expected, received)
# centroids_(d)ict2
received = assign_(p)oints_(t)o_(c)entroids(list_(o)f_(p)oints, centroids_(d)ict2)
expected ={
"centroid1": [[-1.36639346,-0.38664658,-1.02232584,-1.05902604],
[1.13659605,-2.47109085,-0.83996912,-0.24579457],
[-0.83095884,-1.73002213,-0.01361636,-0.3265274],
[-1.01714716,0.95954521,1.20493919,0.34804443],

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!