Hierarchical clustering: single method
Let us use the same footfall dataset and check if any changes are seen if we use a different method for clustering.
The data is stored in a pandas DataFrame, comic_con
. x_scaled
and y_scaled
are the column names of the standardized X and Y coordinates of people at a given point in time.
This exercise is part of the course
Cluster Analysis in Python
Exercise instructions
- Import
fcluster
andlinkage
fromscipy.cluster.hierarchy
. - Use the
single
method in thelinkage()
function.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import the fcluster and linkage functions
from ____ import ____, ____
# Use the linkage() function
distance_matrix = ____(comic_con[[____, ____]], ____ = ____, metric = ____)
# Assign cluster labels
comic_con['cluster_labels'] = ____(____, ____, ____)
# Plot clusters
sns.scatterplot(x='x_scaled', y='y_scaled',
hue='cluster_labels', data = comic_con)
plt.show()