+1 917 8105386 [email protected]

Updating Assignment

this computer science homework. The language is python, more specifically numpy. This problem I solved already, but you need to understand it to do the next one which is the one I need help on. def update_assignments(num_clusters, X, centers): """ Returns the cluster assignment (number) for each data point in X, computed as the closest cluster center. Parameters ---------- num_clusters : int The number of disjoint clusters (i.e., k) in the X X : numpy array of shape (m, 2) An array of m data points in R^2. centers : numpy array of shape (num_clusters, 2) The coordinates for the centers of each cluster Returns ------- cluster_assignments : numpy array of shape (m,) An array containing the cluster label assignments for each data point in X. Each cluster label is an integer between 0 and (num_clusters - 1). """ closest = [] for i in range(len(X)): dist = [] for j in range(len(centers)): dist.append(distance(X[i], centers[j])) closest.append(dist.index(min(dist))) cluster_assignments = np.array(closest) return cluster_assignments After you understand that, I need help on this one: Now, we need to do the next step of the clustering algorithm: recompute the cluster centers based on which points are assigned to that cluster. Recall that the new centers are simply the two-dimensional means of each group of data points. A two-dimensional mean is calculated by simply finding the mean of the x coordinates and the mean of the y coordinates. Complete the update_parameters function to do this. def update_parameters(num_clusters, X, cluster_assignment): """ Recalculates cluster centers running update_assignments. Parameters ---------- num_clusters : int The number of disjoint clusters (i.e., k) in the X X : numpy array of shape (m, 2) An array of m data points in R^2 cluster_assignment : numpy array of shape (m,) The array of cluster labels assigned to each data point as returned from update_assignments Returns ------- updated_centers : numpy array of shape (num_clusters, 2) An array containing the new positions for each of the cluster centers """

Ready To Get Started?

GET STARTED TODAY