Created
July 1, 2019 16:34
-
-
Save karanjakhar/19f762d58a44753f1ccf39fa89ff3881 to your computer and use it in GitHub Desktop.
k-means with dummy data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#importing required libraries | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn.cluster import KMeans | |
#creating data | |
x1 = np.concatenate((np.random.normal(10,2,(100,1)),np.random.normal(20,5,(100,1)))) | |
x2 = np.concatenate((np.random.normal(10,2,(100,1)), np.random.normal(30,3,(100,1)))) | |
#visualizing the data | |
plt.scatter(x1.flatten(),x2.flatten()) | |
plt.show() | |
#fitting k-means classifier | |
kmeans = KMeans(n_clusters = 2) | |
kmeans.fit(np.concatenate((x1,x2),axis = 1)) | |
#visualizing kmeans result | |
plt.scatter(x1.flatten(),x2.flatten(),c = kmeans.labels_) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment