diff --git a/exercises/Numpy_KMeansClustering/NumPy_KMeansClustering_stdPython.ipynb b/exercises/Numpy_KMeansClustering/NumPy_KMeansClustering_stdPython.ipynb index 33e38357348697c443aab237abfc8f10036fc6fd..456c8feea6da474385194ae85091e62f0879b307 100644 --- a/exercises/Numpy_KMeansClustering/NumPy_KMeansClustering_stdPython.ipynb +++ b/exercises/Numpy_KMeansClustering/NumPy_KMeansClustering_stdPython.ipynb @@ -139,6 +139,38 @@ " return coords_center" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "662132b4", + "metadata": {}, + "outputs": [], + "source": [ + "# return the updated locations of the cluster centers\n", + "def compute_centers_efficient(coords, labels, n_centers):\n", + " # Provide your implementation here. \n", + " # **HINT**:\n", + " # \n", + " # Use advanced indexing with boolean masks to access\n", + " # all points that have a label corresponding to the \n", + " # index of a cluster center.\n", + " new_center_coords=[[0,0] for x in range(n_centers)]\n", + " number_of_points=[0 for x in range(n_centers)]\n", + " \n", + " for label, coordinate in zip (labels,coords):\n", + " new_center_coords[label][1]+=coordinate[1]#y\n", + " new_center_coords[label][0]+=coordinate[0]#x\n", + " number_of_points[label]+=1\n", + " \n", + " for coord,num_point in zip(new_center_coords,number_of_points):\n", + " assert num_point != 0, \"Error - found cluster size with value 0.\"\n", + " coord[1]=coord[1]/num_point\n", + " coord[0]=coord[0]/num_point\n", + " \n", + " return new_center_coords\n", + " " + ] + }, { "cell_type": "code", "execution_count": null, @@ -283,7 +315,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" },