Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele What is a Spectral Clustering? | Spectral Clustering
Cluster Analysis in Python

book
What is a Spectral Clustering?

Welcome to the fourth section of the course! In this section, we will learn the last (but not the last overall) clustering method.

This method is heavily based on math, such as eigenvalues, laplacians, graphs, and kernels. We will not dig into the algorithm itself but will consider the main cases of its usage.

You might forget all the charts you saw in the course, but in the second section, we considered an interesting set of points. Look at the scatterplot below.

And look below at the results of the K-Means algorithm.

Well, that's not what we were looking for. So, how can you implement the spectral clustering algorithm in Python? The key function is SpectralClustering from sklearn.cluster. The algorithm of prediction is the same as in the previous section:

  1. Create SpectralClustering model with k clusters using n_clusters = k parameter.

  2. Fit the numerical data and predict the labels using the .fit_predict() method of model and passing numerical data as a parameter.

Then, if needed, you can visualize the results.

Let's find out can the algorithm heavily based on strong math correctly define the clusters?

Tehtävä

Swipe to start coding

  1. Import SpectralClustering function from sklearn.cluster.
  2. Create a SpectralClustering model with 2 clusters named model.
  3. Fit the data and predict the labels using model. Save predicted labels within the 'prediction' column of the data.
  4. Build a scatter plot (using seaborn) with 'x' column on the x-axis, 'y' column on the y-axis, and each point colored with respect to the 'prediction' column of the data.

Ratkaisu

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

# 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 = SpectralClustering(n_clusters = 2)

# Fit the data and predict the labels
data['prediction'] = model.fit_predict(data)

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 1
single

single

# Import the libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from ___ import ___

# 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 = ___(___ = ___)

# Fit the data and predict the labels
data['prediction'] = model.___(___)

# Visualize the results
sns.___(x = '___', y = '___', hue = '___', data = data)
plt.show()

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt