Question
I need help coding this in Python!! **Note this is all one question and needs to be implemented using only packages imported below (CANNOT use
I need help coding this in Python!!
**Note this is all one question and needs to be implemented using only packages imported below (CANNOT use any other packages such as seaborn or sklearn for this assignment**
->The description of what each method is in """quotations""" is the objective of the homework.
Thanks in advance!!!
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
%matplotlib inline
import sys
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import imageio
from tqdm import tqdm_notebook as tqdm
class KMeans(object):
def __init__(self):
pass
def _init_centers(self, points, K, **kwargs):
"""
Args:
points: NxD numpy array, where N is # 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
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 is the dimension.
Note that it is possible to have fewer centers after this step.
"""
raise NotImplementedError
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):
"""
Args:
points: NxD numpy array, where N is # points and D is 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, K, **kwargs)
pbar = tqdm(range(max_iters))
for it in pbar:
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[0]
if it:
diff = np.abs(prev_loss - loss)
if diff < abs_tol and diff / prev_loss < rel_tol:
break
prev_loss = loss
pbar.set_description('iter %d, loss: %.4f' % (it, loss))
return cluster_idx, centers
Step 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