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

Time：2022-5-23

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``````

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()``````

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

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

plt.show``````

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

The most popular opinion

Implementation of firewall technology by iptables

This is my favorite teacher Yao’s experimental class for us. We have learned a lot of dry goods. Write a blog to record an important step forward on the road of information security learning! In fact, I wanted to continue to learn the vulnerability of eternal blue today, but because I haven’t finished my firewall […]