Created
April 19, 2019 17:39
-
-
Save tyliec/fdff325a67571dc1f067a2edf51b60d3 to your computer and use it in GitHub Desktop.
K-means
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
# 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