question archive Use the two functions you implemented to calculate the accuracy for every cluster and the whole algorithm, defined as above
Subject:Computer SciencePrice:3.86 Bought12
Use the two functions you implemented to calculate the accuracy for every cluster and the whole algorithm, defined as above. Implement the following function in analysis.py:
def accuracy(data, labels, centroids): """ Calculate the accuracy of the algorithm. You should use update_assignment and majority_count (that you previously implemented) Arguments: data: a list of lists representing all data points labels: a list of ints representing all data labels centroids: the centroid dictionary Returns: a float representing the accuracy of the algorithm """
I have already implemented:
def update_assignment(data, labels, centroids):
closest = {}
for centroid in centroids:
closest[centroid] = []
for label, point in zip(labels, data):
centroid = assign_data(point, centroids)
closest[centroid].append(label)
return {centroid: points for centroid, points in closest.items()
if len(points) > 0}
def majority_count(labels):
maj = {}
for label in labels:
if label in maj.keys():
maj[label] += 1
else:
maj[label] = 1
v = list(maj.values())
return max(v)
Purchased 12 times