100% found this document useful (1 vote)
11 views2 pages

Cluster Comparison: K-means vs Agglomerative

The document outlines a function named 'my_cluster_comparison' that performs clustering on a training matrix using K-means and agglomerative clustering. It describes the process of mapping agglomerative cluster labels to K-means labels by computing centroids, applying a nearest neighbor model, and updating the training vectors. The function returns the indices where the agglomerative and K-means cluster labels differ, with an example test case provided for clarity.

Uploaded by

vinaynaidu6872
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
11 views2 pages

Cluster Comparison: K-means vs Agglomerative

The document outlines a function named 'my_cluster_comparison' that performs clustering on a training matrix using K-means and agglomerative clustering. It describes the process of mapping agglomerative cluster labels to K-means labels by computing centroids, applying a nearest neighbor model, and updating the training vectors. The function returns the indices where the agglomerative and K-means cluster labels differ, with an example test case provided for clarity.

Uploaded by

vinaynaidu6872
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a func on named my_cluster_comparison that will cluster an input training matrix using both

K-means clustering and agglomera ve clustering. To compare the cluster members for each
technique, it is unlikely that the computed cluster labels will end up being the same. To resolve this
issue, your func on will map the agglomera ve clustering labels to the K-means clustering labels as
follows (assuming the same number of clusters for each technique): A er using the input data to fit
both an Agglomera veClustering model and a KMeans model, for each agglomera ve cluster.

1. Compute the centroid of the agglomera ve cluster (such as by using the


numpy mean method).

2. Using the centroids obtained by fi ng the KMeans model to the input training set, apply
the neighbors method to fit a nearest neighbor model using one neighbor.

3. Use the predict method from your nearest neighbor object to classify the agglomera ve
centroid computed in step 1. This step will correctly associate the agglomera ve cluster with
the appropriate KMeans label.

4. Update the input training vectors with the new agglomera ve label.

5. Return the training vector indices where the new agglomera ve indices differ from the
Kmeans indices.

Func on call syntax results = my_cluster_comparison(X_train, nc, random_state_val)

 X_train = input p×np×n numpy matrix containing pp samples and nn features.

 nc = integer describing the number of clusters

 random_state_val= integer for se ng the random state

You may assume

import numpy as np
from [Link] import KMeans
from [Link] import Agglomera veClustering
from sklearn import neighbors
from sklearn.model_selec on import train_test_split

have been invoked.

Example test case:

random_state_val = 50
[Link](seed=random_state_val)

npts = 25

dim = 2

a=1

c0_mu = [Link]([a,a])
c0_data = c0_mu + [Link](0, 1, size=(npts, dim))

c1_mu = [Link]([-a,a])

c1_data = c1_mu + [Link](0, 1, size=(npts, dim))

c2_mu = [Link]([a,-a])

c2_data = c2_mu + [Link](0, 1, size=(npts, dim))

c3_mu = [Link]([-a,-a])

c3_data = c3_mu + [Link](0, 1, size=(npts, dim))

X = [Link]((c0_data, c1_data, c2_data, c3_data), axis=0)

nc = 4

X_train, X_test = train_test_split(X, test_size=0.20, random_state=random_state_val)

results = my_cluster_comparison(X_train, nc, random_state_val)

should return a value of

(array([ 1, 2, 9, 12, 13, 14, 28, 29, 34, 42, 47, 57, 59, 60, 66, 67, 74]),)

for results.

Notes:

 Try to keep the computed labels as type integer

Answer:(penalty regime: 0 %)

Common questions

Powered by AI

The 'my_cluster_comparison' function requires the input data to be a p×n numpy matrix containing p samples and n features. This structure is necessary because both K-means and agglomerative clustering require numerical input data to calculate centroids and perform clustering operations. Additionally, an integer value 'nc' specifies the number of clusters, and another integer 'random_state_val' for setting the random state ensures reproducibility .

The expected output of the 'my_cluster_comparison' function is a tuple containing an array of indices. These indices represent positions in the input training vector where the newly mapped agglomerative clustering labels differ from the original KMeans labels. The significance of this output is that it highlights discrepancies in cluster assignments between the two clustering techniques, thus informing potential adjustments or insights into data structure and clustering efficacy .

The nearest neighbor model is used in the 'my_cluster_comparison' function to map agglomerative clustering centroids to the nearest K-means cluster centroid labels. Once the centroids for the agglomerative clustering are computed, the nearest neighbor model with k=1 is fitted using the KMeans centroids. This model predicts which KMeans cluster is closest to each agglomerative centroid, effectively aligning and standardizing cluster labeling between the two methods. This process ensures that the clusters from different algorithms can be sensibly compared .

The 'random_state_val' parameter in the 'my_cluster_comparison' function is used to set the random state seed for both creating reproducible test and train data splits and to ensure consistency and reproducibility across executions of KMeans clustering. This controlled randomness is crucial for consistent clustering outcomes, especially during model parameter tuning or iterative testing .

To resolve or investigate discrepancies between agglomerative and K-means clusterings indicated by the function, one might take several steps: First, investigate the data distribution and check for inherent factors such as noise or outliers that could influence clustering differences. Next, vary the number of clusters to see if more or fewer clusters harmonize the results. Adjusting parameters such as linkage criteria in agglomerative clustering or initializing different centroids in K-means might also help. Additionally, utilizing visualization techniques to analyze the shape and structure of clusters can provide further insights, ultimately guiding more informed decisions on data preprocessing or method adjustments .

Employing both K-means and agglomerative clustering methods in the 'my_cluster_comparison' function allows for a more robust analysis by leveraging the strengths of both techniques. K-means is efficient for large datasets and works well with spherical clusters, while agglomerative clustering excels in revealing hierarchical structures within data. By mapping the labels from agglomerative clustering to those of K-means, the function provides a mechanism to evaluate and validate clustering consistency, thereby enhancing the reliability and insights derived from clustering analyses .

It is unlikely that the computed cluster labels from K-means and agglomerative clustering will be the same because these two algorithms use fundamentally different methods to form clusters; K-means partitions data into clusters by minimizing within-cluster variance, while agglomerative clustering builds hierarchies based on point or cluster similarities. The function addresses this discrepancy by mapping the labels of agglomerative clustering onto those of K-means through the use of nearest neighbor classification based on cluster centroids, thus ensuring label consistency between the two methods .

The primary purpose of the 'my_cluster_comparison' function is to cluster an input training matrix using both K-means clustering and agglomerative clustering, and to resolve the issue of having different computed cluster labels by mapping the agglomerative clustering labels to the K-means clustering labels. This is achieved by calculating the centroids for each agglomerative cluster and using a nearest neighbor model to classify these centroids, effectively associating each agglomerative cluster with the appropriate KMeans label .

The function 'my_cluster_comparison' maps agglomerative clustering labels to K-means labels by first computing the centroid of each agglomerative cluster using the numpy mean method. Then, it fits a nearest neighbor model with one neighbor using the centroids obtained from the KMeans model to the input training set. The predict method of the nearest neighbor model is then used to classify each agglomerative cluster centroid, thereby associating it with the corresponding KMeans label .

In the 'my_cluster_comparison' function, cluster centroids are calculated by taking the mean of all data points within each agglomerative cluster, using numpy's mean function. These centroids represent the central point of each cluster. The centroids are then utilized by feeding them into a nearest neighbor model alongside KMeans cluster centroids to determine the nearest KMeans cluster, effectively mapping agglomerative cluster labels to KMeans labels .

You might also like