Created
July 2, 2020 18:44
-
-
Save sicongzhao/463457b638ebfcfea395331f7de4db8b to your computer and use it in GitHub Desktop.
This file contains 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
from sklearn.cluster import KMeans | |
import numpy as np | |
# Generate data to be clustered | |
X = np.array([[1, 2], [1, 4], [1, 0], | |
[10, 2], [10, 4], [10, 0]]) | |
# Init K-menas model and clustering | |
kmeans = KMeans(n_clusters=2).fit(X) | |
# Get labels for each cluster | |
kmeans.labels_ | |
# Output: | |
# array([1, 1, 1, 0, 0, 0], dtype=int32) | |
# Predict new data point | |
kmeans.predict([[0, 0], [12, 3]]) | |
# Output: | |
# array([1, 0], dtype=int32) | |
# Get centroid of clusters | |
kmeans.cluster_centers_ | |
# Outcome: | |
# array([[10., 2.], | |
# [ 1., 2.]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment