Answered step by step
Verified Expert Solution
Question
1 Approved Answer
using python. This is all one problem for Kmeans implementation. I need to fill in the red parts in each section and have been trying
using python.
This is all one problem for Kmeans implementation. I need to fill in the red parts in each section and have been trying for days and need some help.
Thank you!
In [6]: | class KMeans(object): def init (self): pass def _init_centers(self, points, K, **kwargs): Args: points: NxD numpy array, where N s # points and D is the dimensionality K: number of clusters kwargs: any additional arguments you want Return: centers: ?x D numpy array, the centers. Note that it is possible that we just need fewer centers (e.g., 1 cluster is enough when all the points are the same). raise NotImplementedError def _update_assignment (self, centers, points): Args: centers: KxD numpy array, where K is the number of clusters, and D is the dimension points: NXD numpy array, the observations Return: cluster_idx: numpy array of length N, the cluster assignment for each point raise NotImplementedError cluscerX numpy array o lengeh N> the causter ass^rment or each poh raise NotImplementedError def _update_centers (self, old_centers, cluster_idx, points): Args: old centers: old centers KxD numpy array, where K is the number of clusters, and D is the dimension cluster_idx: numpy array of length N, the cluster assignment for each point points: NxD numpy array, the observations Return: centers: new centers, ? x D numpy array, where K is the number of clusters, and D s the dimension. Note that it is possible to have fewer centers after this step. raise NotImplementedError n this cell def_get_loss (self, centers, cluster_idx, points): Args: centers: KxD numpy array, where K is the number of clusters, and D is the dimension cluster_idx: numpy array of length N, the cluster assignment for each point points: NXD numpy array, the observations Return: loss: a single float number, which is the objective function of KMeans. raise NotImplementedError def call (self, points, K, max iters-100 . abs tol-1e-16, rel tol=1e-16, **kwargs): def_cal1_(self, points, K, max_iters-100, abs_tol-1e-16, rel_tol-1e-16, **kwargs): Args: points: NXD numpy array, where N s # points and D s the dimensionality K: number of clusters max_iters: maximum number of iterations abs tol: convergence criteria w.r.t absolute change of loss rel tol: convergence criteria w.r.t relative change of loss kwargs: any additional arguments you want Return: cluster assignments: Nx1 int numpy array cluster centers: ?x D numpy array, the centers centers = self-init-centers(points, pbar tqdm(range(max_iters)) for it in pbar: K, **kwargs) cluster_idx - self._update_assignment (centers, points) centers self._update_centers(centers, cluster_idx, points) loss = self-get-loss(centers, cluster-idx, points) K = centers. shape[e] if it: diff- np . abs (prev-loss - loss) if diff abs_tol and diff / prev-lossStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started