Python implements spectral clustering algorithm and visual comparison of the results of changing the number of clusters

Time:2022-5-23

Original link:http://tecdat.cn/?p=24842 

Spectral clustering is a technology that applies the spectrum of the similarity matrix of data to dimensionality reduction. It is a useful and easy to implement clustering method.

The scikit learn API provides spectral clustering to implement the spectral clustering method in Python. Spectral clustering applies clustering to the projection of normalized Laplace operator. In this tutorial, we will briefly learn how to cluster and visualize data using spectral clustering in Python. The tutorial covers:

  1. Prepare data
  2. Clustering using spectral clustering and visualization
  3. source code

We will first import the required libraries and functions.

from numpy import random

Prepare data

We will use make_ The blob () function generates a simple data set and visualizes it in the graph.

random.seed
make_blobs

plt
plt.show

Python implements spectral clustering algorithm and visual comparison of the results of changing the number of clusters

This is an easy to understand data, so we will use spectral clustering method to cluster it.

Spectral clustering and visualization

We will use spectral clustering to define the model, and then we will fit it to the X data. Spectral clustering requires the number of clusters, so set 4 to n_ Cluster parameter. You can examine the parameters of the class and change them based on your analysis and target data.

SptlCltg.fit(x)


SelCg( n_clusters=4)

Next, we will visualize the clustering data in the graph. In order to distinguish clustering by color, we will extract label data from the fitting model.

labels = sc.labels_

plt.scatter(x\[:,0\], x\[:,1\], c=labels)
plt.show()

Python implements spectral clustering algorithm and visual comparison of the results of changing the number of clusters

We can also check the clustering results by changing the number of clusters.

plt
f.add_subplot
for i in range:
 sc = Serurg.fit
 f.add_subplot
 plt.scatter
 plt.legen

plt.show

Python implements spectral clustering algorithm and visual comparison of the results of changing the number of clusters

In this tutorial, we briefly learned how to cluster and visualize data in Python.


Python implements spectral clustering algorithm and visual comparison of the results of changing the number of clusters

The most popular opinion

1.R language K-shape algorithm stock price time series clustering

2.Comparison of different types of clustering methods in R language

3.Using R language, k-medoids clustering modeling and gam regression are carried out for power load time series data

4.Hierarchical clustering of iris data set in R language

5.Python Monte Carlo K-means clustering practice

6.Clustering web comment text mining with R

7.Python for NLP: multi label text LSTM neural network using keras

8.Analysis of MNIST data set with R language and exploration of handwritten numeral classification data

9.Deep learning image classification of small data set based on keras in R language