fix error in the loop # UNQC2 GRADED FUNCTION: computecentroids def computecentroids(X,…
- User
fix error in the loop # UNQ_C2
GRADED FUNCTION: compute_centroids
def compute_centroids(X, idx, K): """ Returns the new centroids by computing the means of the data points assigned to each centroid.
Args: X (ndarray): (m, n) Data points idx (ndarray): (m,) Array containing index of closest centroid for each example in X. Concretely, idx[i] contains the index of the centroid closest to example i K (int): number of centroids Returns: centroids (ndarray): (K, n) New centroids computed """ # Useful variables m, n = X.shape # You need to return the following variables correctly centroids = np.zeros((K, n))for k in range(K): # Find all data points assigned to centroid k # idx == k creates a boolean mask where True indicates points assigned to centroid k points_in_cluster = X[idx == k]
# If there are points assigned to this centroid if len(points_in_cluster) > 0: # Compute the mean of all points assigned to centroid k centroids[k] = np.mean(points_in_cluster, axis=0) return centroids; error- File "<tokenize>", line 26 for k in range(K): ^
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.