Skip to content

Instantly share code, notes, and snippets.

@debonx
Last active August 21, 2019 10:20
Show Gist options
  • Save debonx/2282062fa3ae6e3e5891b884c17f7e77 to your computer and use it in GitHub Desktop.
Save debonx/2282062fa3ae6e3e5891b884c17f7e77 to your computer and use it in GitHub Desktop.
Example of Unsupervised Machine Learning with KMeans (sklearn).
import matplotlib.pyplot as plt
from sklearn import datasets
# From sklearn.cluster, import Kmeans class
from sklearn.cluster import KMeans
# Load data
iris = datasets.load_iris()
samples = iris.data
# Use KMeans() to create a model that finds 3 clusters
model = KMeans(n_clusters = 3)
# Use .fit() to fit the model to samples
model.fit(samples)
# Use .predict() to determine the labels of samples
labels = model.predict(samples)
# Print the labels
print(labels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment