Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте K-Medoids and the Weather Data | K-Medoids Algorithm
Cluster Analysis in Python

book
K-Medoids and the Weather Data

As you can see, there was no such clear peek as in the example. That means that both 3 and 4 clusters may be a good choice!

Let's see what will be the result of using the K-Medoids algorithm for the weather data we used in the previous section. Let's start with defining the optimal number of clusters.

Завдання

Swipe to start coding

Given cities' average temperatures dataset data. The numerical columns are 3 - 14. Table

Your tasks are:

  1. Using for loop iterate over n_cl. Within the loop:
  • Create KMedoids model with j clusters named model.
  • Fit the 2-15 columns of data to the model. Watch out that indices in Python start from 0.
  • Add silhouette score value to the silhouettes list. Remember to pass two parameters: the data used for fitting (the same 3-15 columns) and predicted by model labels.
  1. Visualize the results using lineplot of sns. Pass n_cl as x parameter and silhouettes as y. Do not forget to apply the .show() method to display the plot.

Рішення

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

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

# Creating lists
n_cl = range(2, 10)
silhouettes = []

# Calculate the scores for different number of clusters
for j in n_cl:
model = KMedoids(n_clusters = j)
model.fit(data.iloc[:,2:-1])
silhouettes.append(silhouette_score(data.iloc[:,2:-1], model.labels_))
# Visualize the results
g = sns.lineplot(x = n_cl, y = silhouettes)
g.set_xlabel('Number of clusters')
g.set_ylabel('Silhouette score')
plt.show()

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 4
single

single

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

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

# Creating lists
n_cl = range(2, 10)
silhouettes = []

# Calculate the scores for different number of clusters
for j in ___:
model = ___(___ = ___)
model.fit(data.___[:,___])
silhouettes.append(___(___.___[:,___], model.___))
# Visualize the results
g = sns.___(x = ___, y = ___)
g.set_xlabel('Number of clusters')
g.set_ylabel('Silhouette score')
___

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt