Let’s imagine for second that we are beekeepers. We have a swarm of bees buzzing around and our objective is to group them into K distinct hives (i.e., our clusters).

To start, we randomly pick K bees. These bees will act as our cluster centroids.

Like bees drawn to honey, each bee (data point) will gravitate towards the nearest hive (centroid).

After all the bees have found a hive, we’ll determine the new centroid of each hive (update centroids). We’ll keep repeating this process until the bees settle down and stop switching hives.

And voila, that’s the essence of K-means clustering!

*Pros*

- Simple and easy to understand
- Easily scalable
- Efficient in terms of computational cost

*Cons*

- You need to specify K in advance
- Sensitive to the initial selection of centroids
- Assumes clusters are spherical and equally sized (which may not always be the case)

## Perfect Use-Cases

- Market segmentation
- Document clustering
- Image segmentation
- Anomaly detection

## Python Example

`from sklearn.cluster import KMeans`

import numpy as np# Let's assume we have some data

X = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])

# We initialize KMeans with the number of clusters we want

kmeans = KMeans(n_clusters=2, random_state=0).fit(X)

# We can get the labels of the data points

print(kmeans.labels_)

# And we can predict the clusters for new data points

print(kmeans.predict([[0, 0], [4, 4]]))

# The cluster centers (the mean of all the points in that cluster) can be accessed with

print(kmeans.cluster_centers_)

Suppose that you’re attending a large family wedding where the familial connections are unclear.

Your first task is to identify the immediate family members, like siblings or parents and children, and bring them together.

Following this, you hunt for other relations who share a close bond with these established groups and incorporate them.

You continue this process, gradually piecing together the whole family and friends tapestry until everyone is interconnected.

And voila, that’s the essence of hierarchical clustering!

## Pros

- No need to specify the number of clusters
- Provides a hierarchy of clusters which can be useful

## Cons

- Computationally expensive for large datasets
- Sensitive to the choice of distance measure

## Perfect Use-Cases

- Gene sequencing
- Social network analysis
- Building taxonomy trees

## Python Example

`from sklearn.cluster import AgglomerativeClustering`

import numpy as np# Let's assume we have some data

X = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])

# We initialize AgglomerativeClustering with the number of clusters we want

clustering = AgglomerativeClustering(n_clusters=2).fit(X)

# We can get the labels of the data points

print(clustering.labels_)