Spectral Clustering works by transforming the data into a subspace prior to clustering. This is incredibly useful when the data is high dimensional. This saves the effort of doing a PCA or a dimensionality reduction ourselves prior to clustering. Spectral clustering works by determining an affinity matrix between the datasets. The data is represented as a graph and an affinity matrix is computed. For the affinity function, we can use the rbf kernel function or nearest neighbors.
Let us consider the intertwined circles with noise:
from sklearn.cluster import SpectralClustering
from sklearn import datasets
import pandas as pd
import seaborn as sns
N_Samples = 2000
X, y = datasets.make_circles(n_samples=N_Samples, factor=.5, noise=.2)
noisy_circles = pd.DataFrame({'X_0':X[:,0],'X_1':X[:,1], 'y':y})
# Fit the spectral clustering to the dataset and plot the results.
spectral = SpectralClustering(n_clusters=2, eigen_solver='arpack', affinity="nearest_neighbors")
noisy_circles.drop('y', 1)
use .fit and .labels_ to extract the cluster associations.
spectral.fit(noisy_circles)
noisy_circles['spectral'] = spectral.labels_
g=sns.pairplot(x_vars="X_0", y_vars="X_1", hue = "spectral", data = noisy_circles)
g.fig.set_size_inches(14, 6)
sns.despine()
ref_tmp_var = False
try:
ref_assert_var = False
import numpy as np
spectral_ = SpectralClustering(n_clusters=2, eigen_solver='arpack', affinity="nearest_neighbors")
spectral_.fit(noisy_circles)
if (len(noisy_circles['spectral']) == len(spectral_.labels_)):
ref_assert_var = True
out = g
else:
ref_assert_var = False
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
else:
if ref_assert_var:
ref_tmp_var = True
else:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var
# This section will contain code examples that you can run to generate results as well as quizzes.
#
#
ref_tmp_var = False
try:
ref_assert_var = True
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions.')
else:
if ref_assert_var:
ref_tmp_var = True
else:
print('Please follow the instructions given and use the same variables provided in the instructions.')
assert ref_tmp_var