Title: k-Means Clustering
Slug: k-means_clustering
Summary: How to conduct k-means clustering in scikit-learn.
Date: 2017-09-22 12:00
Category: Machine Learning
Tags: Clustering
Authors: Chris Albon
In [1]:
# Load libraries
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
In [2]:
# Load data
iris = datasets.load_iris()
X = iris.data
In [3]:
# Standarize features
scaler = StandardScaler()
X_std = scaler.fit_transform(X)
In [4]:
# Create k-mean object
clt = KMeans(n_clusters=3, random_state=0, n_jobs=-1)
# Train model
model = clt.fit(X_std)
In [5]:
# View predict class
model.labels_
Out[5]:
In [6]:
# Create new observation
new_observation = [[0.8, 0.8, 0.8, 0.8]]
In [7]:
# Predict observation's cluster
model.predict(new_observation)
Out[7]:
In [8]:
# View cluster centers
model.cluster_centers_
Out[8]: