Clustering Examples

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from IPython import display

%matplotlib inline

# Make the images larger
plt.rcParams['figure.figsize'] = (16, 9)

Generate Example 1 Data

4 clusters in the image below.

X, y = make_blobs(n_samples=500, n_features=3, centers=4, random_state=5)

fig = plt.figure();
ax = Axes3D(fig)
ax.scatter(X[:, 0], X[:, 1], X[:, 2]);

png

Generate Example 2 Data

2 clusters in the image below.

Z, y = make_blobs(n_samples=500, n_features=5, centers=2, random_state=42)

fig = plt.figure()
plt.scatter(Z[:, 0], Z[:, 1]);

png

Generate Example 3 Data

Appears to be 6 clusters in the image below.

T, y = make_blobs(n_samples=500, n_features=5, centers=8, random_state=5)

fig = plt.figure();
ax = Axes3D(fig)
ax.scatter(T[:, 1], T[:, 3], T[:, 4]);

png

Plot data for Example 4

But, this is the same data as in Example 3. We see that rotated slightly there are actually at least 7 groups in this data.

fig = plt.figure();
ax = Axes3D(fig)
ax.scatter(T[:, 1], T[:, 2], T[:, 3]);

png