Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Cluster Centers | K-Medoids Algorithm
Cluster Analysis in Python

book
Cluster Centers

In the previous chapter, we compared the K-Means and K-Medoids algorithms. It was mentioned that, unlike K-Means, the K-Medoids algorithm has data points as medoids. Let's visualize the differences.

For example, for the training dataset with three clusters we can visualize the clusters and their centers for both K-Means and K-Medoids. The results are displayed below.

If you watch closely to the right chart and compare it with the central one, you will notice that marked points are data points, unlike the points on the left chart. I've marked these points with green arrows. Let's represent the difference using a more obvious example! The result of clustering using the K-Means algorithm, and cluster centers (labeled with 10) are displayed below.

Tarefa

Swipe to start coding

For the same dataset of points data compute the K-Medoids algorithm, and display the cluster centers. Follow the next steps:

  1. Import KMedoids function from sklearn_extra.cluster library.
  2. Create KMedoids model model with 2 clusters.
  3. Fit the data to the model.
  4. Add 'prediction' column with predicted by model labels to data.
  5. Use the .cluster_centers_ method to extract the clusters centers array.
  6. Visualize the clusters (already done) and centers. Within the second .scatterplot function set hue to 10, and s to 150.

Solução

# Import the libraries
import pandas as pd
from sklearn_extra.cluster import KMedoids
import matplotlib.pyplot as plt
import seaborn as sns

# Read the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/138ab9ad-aa37-4310-873f-0f62abafb038/model_data3.csv', index_col = 0)

# Create the model
model = KMedoids(n_clusters = 2)

# Fit the data to the model
model.fit(data)

# Add predicted labels to data
data['prediction'] = model.predict(data)

# Save clusters centers
centers = pd.DataFrame({'x': model.cluster_centers_[:,:1].reshape(2), 'y': model.cluster_centers_[:,1:].reshape(2)})

# Visualize the results
sns.scatterplot(x = 'x', y = 'y', hue = 'prediction', data = data)
sns.scatterplot(x = 'x', y = 'y', hue = 10, s = 150, data = centers)
plt.show()

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from ___

# Read the data
data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/138ab9ad-aa37-4310-873f-0f62abafb038/model_data3.csv', index_col = 0)

# Create the model
model = ___(___ = 2)

# Fit the data to the model
___.___(data)

# Add predicted labels to data
data['prediction'] = ___.___(data)

# Save clusters centers
centers = pd.DataFrame({'x': model.___[:,:1].reshape(2),
'y': model.___[:,1:].reshape(2)})
# Visualize the results
sns.scatterplot(x = 'x', y = 'y', hue = 'prediction', data = data)
sns.scatterplot(x = 'x', y = 'y', hue = ___, s = ___, data = centers)
plt.show()

Pergunte à IA

expand
ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt