Skip to content

Instantly share code, notes, and snippets.

@tyliec
Created April 19, 2019 17:39
Show Gist options
  • Save tyliec/fdff325a67571dc1f067a2edf51b60d3 to your computer and use it in GitHub Desktop.
Save tyliec/fdff325a67571dc1f067a2edf51b60d3 to your computer and use it in GitHub Desktop.
K-means
# import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
X = -2 * np.random.rand(100, 2)
X1 = 1 + 2 * np.random.rand(50, 2)
X[50:100, :] = X1
plt.scatter(X[:,0], X[:,1], s = 50, c = 'b')
plt.show()
from sklearn.cluster import KMeans
Kmean = KMeans(n_clusters=2)
Kmean.fit(X)
# Kmean.cluster_centers_
plt.scatter(X[:,0], X[:,1], s = 50, c = 'b')
plt.scatter(-0.94665068, -0.97138368, s = 200, c = 'g', marker = 's')
plt.scatter(2.01559419, 2.02597093, s = 200, c = 'r', marker = 's')
plt.show()
# Kmean.labels_
sample_test=np.array([-3.0,-3.0])
second_test=sample_test.reshape(1, -1)
Kmean.predict(second_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment