Pokémon sightings: hierarchical clustering
We are going to continue the investigation into the sightings of legendary Pokémon from the previous exercise. Remember that in the scatter plot of the previous exercise, you identified two areas where Pokémon sightings were dense. This means that the points seem to separate into two clusters. In this exercise, you will form two clusters of the sightings using hierarchical clustering.
'x'
and 'y'
are columns of X and Y coordinates of the locations of sightings, stored in a pandas DataFrame, df
. The following are available for use: matplotlib.pyplot
as plt
, seaborn
as sns
, and pandas
as pd
.
Diese Übung ist Teil des Kurses
Cluster Analysis in Python
Anleitung zur Übung
- Import the
linkage
andfcluster
libraries. - Use the
linkage()
function to compute distances using the ward method. - Generate cluster labels for each data point with two clusters using the
fcluster()
function. - Plot the points with seaborn and assign a different color to each cluster.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Import linkage and fcluster functions
from scipy.cluster.hierarchy import ____, ____
# Use the linkage() function to compute distance
Z = ____(____, 'ward')
# Generate cluster labels
df['cluster_labels'] = ____(____, ____, criterion='maxclust')
# Plot the points with seaborn
sns.scatterplot(x=____, y=____, hue=____, data=df)
plt.show()