Skip to content

Instantly share code, notes, and snippets.

@kushalvyas
Last active July 19, 2016 17:00
Show Gist options
  • Save kushalvyas/9c6a64a467e7e2c2481ad640061b142c to your computer and use it in GitHub Desktop.
Save kushalvyas/9c6a64a467e7e2c2481ad640061b142c to your computer and use it in GitHub Desktop.
"""
Using SKLearns API for performing Kmeans clustering.
Using sklearn.datasets.make_blobs for generating randomized gaussians
for clustering.
"""
import numpy as np
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
# create a dataset sample space that will be used
# to test KMeans. Use function : make_blobs
n_samples = 1000
n_features = 5;
n_clusters = 3;
# aint this sweet
X, y = make_blobs(n_samples, n_features)
# X => array of shape [nsamples,nfeatures] ;;; y => array of shape[nsamples]
# X : generated samples, y : integer labels for cluster membership of each sample
# performing KMeans clustering
ret = KMeans(n_clusters = n_clusters).fit_predict(X)
print ret
__, ax = plt.subplots(2)
ax[0].scatter(X[:,0], X[:,1])
ax[1].scatter(X[:,0], X[:,1], c=ret)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment