8000 DOC: use notebook-style for plot_ward_structured_vs_unstructured.py by krishnakoumar27 · Pull Request #23228 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

DOC: use notebook-style for plot_ward_structured_vs_unstructured.py #23228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 50 additions & 25 deletions examples/cluster/plot_ward_structured_vs_unstructured.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,83 +27,108 @@

import time as time

import matplotlib.pyplot as plt

# The following import is required
# for 3D projection to work with matplotlib < 3.2

import mpl_toolkits.mplot3d # noqa: F401

import numpy as np

from sklearn.cluster import AgglomerativeClustering

# %%
# Generate data
# -------------
#
# We start by generating the Swiss Roll dataset.

from sklearn.datasets import make_swiss_roll

# #############################################################################
# Generate data (swiss roll dataset)
n_samples = 1500
noise = 0.05
X, _ = make_swiss_roll(n_samples, noise=noise)
# Make it thinner
X[:, 1] *= 0.5

# #############################################################################
# %%
# Compute clustering
# ------------------
#
# We perform AgglomerativeClustering which comes under Hierarchical Clustering
# without any connectivity constraints.

from sklearn.cluster import AgglomerativeClustering

print("Compute unstructured hierarchical clustering...")
st = time.time()
ward = AgglomerativeClustering(n_clusters=6, linkage="ward").fit(X)
elapsed_time = time.time() - st
label = ward.labels_
print("Elapsed time: %.2fs" % elapsed_time)
print("Number of points: %i" % label.size)
print(f"Elapsed time: {elapsed_time:.2f}s")
print(f"Number of points: {label.size}")

# #############################################################################
# %%
# Plot result
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d", elev=7, azim=-80)
ax.set_position([0, 0, 0.95, 1])
# -----------
# Plotting the unstructured hierarchical clusters.

import matplotlib.pyplot as plt

fig1 = plt.figure()
ax1 = fig1.add_subplot(111, projection="3d", elev=7, azim=-80)
ax1.set_position([0, 0, 0.95, 1])
for l in np.unique(label):
ax.scatter(
ax1.scatter(
X[label == l, 0],
X[label == l, 1],
X[label == l, 2],
color=plt.cm.jet(float(l) / np.max(label + 1)),
s=20,
edgecolor="k",
)
plt.title("Without connectivity constraints (time %.2fs)" % elapsed_time)
_ = fig1.suptitle(f"Without connectivity constraints (time {elapsed_time:.2f}s)")

# %%
# We are defining k-Nearest Neighbors with 10 neighbors
# -----------------------------------------------------

# #############################################################################
# Define the structure A of the data. Here a 10 nearest neighbors
from sklearn.neighbors import kneighbors_graph

connectivity = kneighbors_graph(X, n_neighbors=10, include_self=False)

# #############################################################################
# %%
# Compute clustering
# ------------------
#
# We perform AgglomerativeClustering again with connectivity constraints.

print("Compute structured hierarchical clustering...")
st = time.time()
ward = AgglomerativeClustering(
n_clusters=6, connectivity=connectivity, linkage="ward"
).fit(X)
elapsed_time = time.time() - st
label = ward.labels_
print("Elapsed time: %.2fs" % elapsed_time)
print("Number of points: %i" % label.size)
print(f"Elapsed time: {elapsed_time:.2f}s")
print(f"Number of points: {label.size}")

# #############################################################################
# %%
# Plot result
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d", elev=7, azim=-80)
ax.set_position([0, 0, 0.95, 1])
# -----------
#
# Plotting the structured hierarchical clusters.

fig2 = plt.figure()
ax2 = fig2.add_subplot(121, projection="3d", elev=7, azim=-80)
ax2.set_position([0, 0, 0.95, 1])
for l in np.unique(label):
ax.scatter(
ax2.scatter(
X[label == l, 0],
X[label == l, 1],
X[label == l, 2],
color=plt.cm.jet(float(l) / np.max(label + 1)),
s=20,
edgecolor="k",
)
plt.title("With connectivity constraints (time %.2fs)" % elapsed_time)
fig2.suptitle(f"With connectivity constraints (time {elapsed_time:.2f}s)")

plt.show()
0