Course Content
Cluster Analysis in Python
Cluster Analysis in Python
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:
- Create
SpectralClustering
model withk
clusters usingn_clusters = k
parameter. - 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?
Swipe to start coding
- Import
SpectralClustering
function fromsklearn.cluster
. - Create a
SpectralClustering
model with 2 clusters namedmodel
. - Fit the
data
and predict the labels usingmodel
. Save predicted labels within the'prediction'
column of thedata
. - 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 thedata
.
Solution
Thanks for your feedback!
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:
- Create
SpectralClustering
model withk
clusters usingn_clusters = k
parameter. - 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?
Swipe to start coding
- Import
SpectralClustering
function fromsklearn.cluster
. - Create a
SpectralClustering
model with 2 clusters namedmodel
. - Fit the
data
and predict the labels usingmodel
. Save predicted labels within the'prediction'
column of thedata
. - 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 thedata
.
Solution
Thanks for your feedback!