Sling Academy
Home/Scikit-Learn/Using Scikit-Learn's `SpectralClustering` for Non-Linear Data

Using Scikit-Learn's `SpectralClustering` for Non-Linear Data

Last updated: December 17, 2024

When it comes to clustering algorithms, K-Means is often one of the most cited examples. However, K-Means was primarily designed for linear separations of data. For datasets where non-linear boundaries define the clusters, algorithms based on concepts derived from Spectral Graph Theory, such as Spectral Clustering, can be incredibly powerful. In this article, we will walk through how to use Scikit-Learn’s SpectralClustering to cluster non-linear data effectively.

What is Spectral Clustering?

Spectral Clustering transforms the data into a lower-dimensional space where the clusters are more easily identifiable. This is achieved by using the eigenvectors of the data’s similarity matrix to reduce dimensionalities. Unlike traditional methods, Spectral Clustering can handle complex clusters that are either non-convex or intertwined.

Setting Up Your Environment

Before we dive into the code, you need to install Scikit-Learn if you haven't already. You can do this quickly using pip:

pip install scikit-learn

In addition to Scikit-Learn, we will leverage libraries such as NumPy for numerical operations and Matplotlib for plotting:

pip install numpy matplotlib

Generating Non-Linear Data

For illustration, we'll generate an artificial dataset where traditional clustering methods may struggle:

from sklearn.datasets import make_moons
import matplotlib.pyplot as plt

# Create synthetic data
X, y = make_moons(n_samples=300, noise=0.1, random_state=42)

# Plot the data
plt.scatter(X[:, 0], X[:, 1], s=50)
plt.title('Synthetic Non-Linear Data')
plt.show()

This dataset, known as the 'two moons' dataset, is a popular choice for testing algorithms on non-linear divisions since clusters are shaped as moons encapsulating each other.

Applying Spectral Clustering

With the dataset ready, we can apply Spectral Clustering:

from sklearn.cluster import SpectralClustering

# Configure the Spectral Clustering model
spectral_cluster = SpectralClustering(n_clusters=2, affinity='nearest_neighbors',
                                      assign_labels='kmeans', random_state=42)

# Fit and predict clusters
labels = spectral_cluster.fit_predict(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis', s=50)
plt.title('Clusters found by Spectral Clustering')
plt.show()

In the code above, we set the number of clusters to 2 since we know our sample contains two distinct classes. The affinity parameter represents the similarity metric we're using. Here, nearest_neighbors helps identify these clusters based on a connectivity approach that is more suited for non-linear data.

Tuning and Customizing

It's important to note that Spectral Clustering allows for a variety of customizations. For instance, if you have prior knowledge about the data structure, you might choose a different number of neighbors:

spectral_custom = SpectralClustering(n_clusters=2, affinity='nearest_neighbors',
                                     n_neighbors=10, random_state=42)
custom_labels = spectral_custom.fit_predict(X)

In this version, we modified the parameter n_neighbors to influence how localized this nearest neighbor affinity is. This could significantly alter the discovered clusters, particularly in density-based datasets.

Conclusion

Spectral Clustering is a versatile and robust algorithm applicable to a plethora of clustering tasks faced in non-linear domains. It generally performs better than K-Means-like algorithms in these contexts due to its handling of irregularly shaped clusters. When working with intricate datasets, consider leveraging Spectral Clustering for better performance.

With the increasing complexity of today’s data, having algorithms like Spectral Clustering at your disposal can significantly enhance your analysis toolbox, especially in pattern recognition and signal processing applications.

Next Article: Spectral Co-Clustering in Scikit-Learn Explained

Previous Article: Spectral Biclustering with Scikit-Learn

Series: Scikit-Learn Tutorials

Scikit-Learn

You May Also Like

  • Generating Gaussian Quantiles with Scikit-Learn
  • Spectral Biclustering with Scikit-Learn
  • Scikit-Learn Complete Cheat Sheet
  • ValueError: Estimator Does Not Support Sparse Input in Scikit-Learn
  • Scikit-Learn TypeError: Cannot Broadcast Due to Shape Mismatch
  • AttributeError: 'dict' Object Has No Attribute 'predict' in Scikit-Learn
  • KeyError: Missing 'param_grid' in Scikit-Learn GridSearchCV
  • Scikit-Learn ValueError: 'max_iter' Must Be Positive Integer
  • Fixing Log Function Error with Negative Values in Scikit-Learn
  • RuntimeError: Distributed Computing Backend Not Found in Scikit-Learn
  • Scikit-Learn TypeError: '<' Not Supported Between 'str' and 'int'
  • AttributeError: GridSearchCV Has No Attribute 'fit_transform' in Scikit-Learn
  • Fixing Scikit-Learn Split Error: Number of Splits > Number of Samples
  • Scikit-Learn TypeError: Cannot Concatenate 'str' and 'int'
  • ValueError: Cannot Use 'predict' Before Fitting Model in Scikit-Learn
  • Fixing AttributeError: NoneType Has No Attribute 'predict' in Scikit-Learn
  • Scikit-Learn ValueError: Cannot Reshape Array of Incorrect Size
  • LinAlgError: Matrix is Singular to Machine Precision in Scikit-Learn
  • Fixing TypeError: ndarray Object is Not Callable in Scikit-Learn