Skip to content

Instantly share code, notes, and snippets.

@KhyatiMahendru
Created July 25, 2019 06:51
Show Gist options
  • Save KhyatiMahendru/772eede385d1ef63de13876e6edc909b to your computer and use it in GitHub Desktop.
Save KhyatiMahendru/772eede385d1ef63de13876e6edc909b to your computer and use it in GitHub Desktop.
# import required functions and libraries
from sklearn.datasets import make_circles
from sklearn.neighbors import kneighbors_graph
from sklearn.cluster import SpectralClustering
import numpy as np
import matplotlib.pyplot as plt
# generate your data
X, labels = make_circles(n_samples=500, noise=0.1, factor=.2)
# plot your data
plt.scatter(X[:, 0], X[:, 1])
plt.show()
# train and predict
s_cluster = SpectralClustering(n_clusters = 2, eigen_solver='arpack',
affinity="nearest_neighbors").fit_predict(X)
# plot clustered data
plt.scatter(X[:, 0], X[:, 1], c = s_cluster)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment