MulaiMulai sekarang secara gratis

Pokémon sightings: k-means clustering

We are going to continue the investigation into the sightings of legendary Pokémon from the previous exercise. Just like the previous exercise, we will use the same example of Pokémon sightings. In this exercise, you will form clusters of the sightings using k-means 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.

Latihan ini adalah bagian dari kursus

Cluster Analysis in Python

Lihat Kursus

Petunjuk latihan

  • Import the kmeans and vq functions.
  • Use the kmeans() function to compute cluster centers by defining two clusters.
  • Assign cluster labels to each data point using vq() function.
  • Plot the points with seaborn and assign a different color to each cluster

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import kmeans and vq functions
from scipy.cluster.vq import ____, ____

# Compute cluster centers
centroids,_ = ____(____, ____)

# Assign cluster labels
df['cluster_labels'], _ = ____(____, ____)

# Plot the points with seaborn
sns.scatterplot(x=____, y=____, hue=____, data=df)
plt.show()
Edit dan Jalankan Kode