Skip to content

Instantly share code, notes, and snippets.

@jayeshcp
Created October 17, 2020 13:52
Show Gist options
  • Save jayeshcp/c5503ee602d5f410d4cbd96feb189bf2 to your computer and use it in GitHub Desktop.
Save jayeshcp/c5503ee602d5f410d4cbd96feb189bf2 to your computer and use it in GitHub Desktop.
K Means Clustering in Python
from numpy import unique
from numpy import where
from matplotlib import pyplot
from sklearn.datasets import make_classification
from sklearn.cluster import KMeans
if __name__ == '__main__':
# initialize the data set we'll work with
training_data, _ = make_classification(
n_samples=2000,
n_features=3,
n_informative=2,
n_redundant=0,
n_clusters_per_class=1,
random_state=10
)
# define the model
kmeans_model = KMeans(n_clusters=2)
# assign each data point to a cluster
dbscan_result = kmeans_model.fit_predict(training_data)
# get all of the unique clusters
dbscan_clusters = unique(dbscan_result)
# plot the DBSCAN clusters
for dbscan_cluster in dbscan_clusters:
# get data points that fall in this cluster
index = where(dbscan_result == dbscan_cluster)
# make the plot
pyplot.scatter(training_data[index, 0], training_data[index, 1])
# show the DBSCAN plot
pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment