The purpose of this lab is to become familiar with the algorithm for K-Means clustering with 2-dimensional data.
testWhereFcn
that will practice using the np.where
command. This
command allows us to select the indices of an array such that the
elements of the array satisfies a particular condition. For
example,
a = np.array([1, 2, 3, 4, 5, 6, 7]) b = np.where(a < 3) print(b)returns the tuple
[0, 1]
.
distance
function.
initClusterCenters
function works. This function chooses
k random elements from the data array to become the initial
centers of the clusters.
labelForPt
takes the list of cluster
centers and a point, and determines which cluster center this point
is closest to. Follow the comments in the code to complete this
function.
determineLabels
function. This function loops through
all of the points in the data array and determines which clusters
they belong to. (It calls the labelForPt
function!)
extractCluster
function and update the comments for that function to reflect what
it does.
recalculateCenters
function.
main
function with different numbers of
clusters and iterations. Add more detailed comments before the main
function to describe what happens.