Skip to content

Commit 420da11

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 01ba635cfcd523d01604ae9eb37a13844df6d92e
1 parent dccc703 commit 420da11

File tree

1,175 files changed

+4692
-4113
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,175 files changed

+4692
-4113
lines changed
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# Plot Hierarchical Clustering Dendrogram\n\nThis example plots the corresponding dendrogram of a hierarchical clustering\nusing AgglomerativeClustering and the dendrogram method available in scipy.\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"import numpy as np\n\nfrom matplotlib import pyplot as plt\nfrom scipy.cluster.hierarchy import dendrogram\nfrom sklearn.datasets import load_iris\nfrom sklearn.cluster import AgglomerativeClustering\n\n\ndef plot_dendrogram(model, **kwargs):\n # Create linkage matrix and then plot the dendrogram\n\n # create the counts of samples under each node\n counts = np.zeros(model.children_.shape[0])\n n_samples = len(model.labels_)\n for i, merge in enumerate(model.children_):\n current_count = 0\n for child_idx in merge:\n if child_idx < n_samples:\n current_count += 1 # leaf node\n else:\n current_count += counts[child_idx - n_samples]\n counts[i] = current_count\n\n linkage_matrix = np.column_stack([model.children_, model.distances_,\n counts]).astype(float)\n\n # Plot the corresponding dendrogram\n dendrogram(linkage_matrix, **kwargs)\n\n\niris = load_iris()\nX = iris.data\n\n# setting distance_threshold=0 ensures we compute the full tree.\nmodel = AgglomerativeClustering(distance_threshold=0, n_clusters=None)\n\nmodel = model.fit(X)\nplt.title('Hierarchical Clustering Dendrogram')\n# plot the top three levels of the dendrogram\nplot_dendrogram(model, truncate_mode='level', p=3)\nplt.xlabel(\"Number of points in node (or index of point if no parenthesis).\")\nplt.show()"
30+
]
31+
}
32+
],
33+
"metadata": {
34+
"kernelspec": {
35+
"display_name": "Python 3",
36+
"language": "python",
37+
"name": "python3"
38+
},
39+
"language_info": {
40+
"codemirror_mode": {
41+
"name": "ipython",
42+
"version": 3
43+
},
44+
"file_extension": ".py",
45+
"mimetype": "text/x-python",
46+
"name": "python",
47+
"nbconvert_exporter": "python",
48+
"pygments_lexer": "ipython3",
49+
"version": "3.7.3"
50+
}
51+
},
52+
"nbformat": 4,
53+
"nbformat_minor": 0
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Authors: Mathew Kallada, Andreas Mueller
2+
# License: BSD 3 clause
3+
"""
4+
=========================================
5+
Plot Hierarchical Clustering Dendrogram
6+
=========================================
7+
This example plots the corresponding dendrogram of a hierarchical clustering
8+
using AgglomerativeClustering and the dendrogram method available in scipy.
9+
"""
10+
11+
import numpy as np
12+
13+
from matplotlib import pyplot as plt
14+
from scipy.cluster.hierarchy import dendrogram
15+
from sklearn.datasets import load_iris
16+
from sklearn.cluster import AgglomerativeClustering
17+
18+
19+
def plot_dendrogram(model, **kwargs):
20+
# Create linkage matrix and then plot the dendrogram
21+
22+
# create the counts of samples under each node
23+
counts = np.zeros(model.children_.shape[0])
24+
n_samples = len(model.labels_)
25+
for i, merge in enumerate(model.children_):
26+
current_count = 0
27+
for child_idx in merge:
28+
if child_idx < n_samples:
29+
current_count += 1 # leaf node
30+
else:
31+
current_count += counts[child_idx - n_samples]
32+
counts[i] = current_count
33+
34+
linkage_matrix = np.column_stack([model.children_, model.distances_,
35+
counts]).astype(float)
36+
37+
# Plot the corresponding dendrogram
38+
dendrogram(linkage_matrix, **kwargs)
39+
40+
41+
iris = load_iris()
42+
X = iris.data
43+
44+
# setting distance_threshold=0 ensures we compute the full tree.
45+
model = AgglomerativeClustering(distance_threshold=0, n_clusters=None)
46+
47+
model = model.fit(X)
48+
plt.title('Hierarchical Clustering Dendrogram')
49+
# plot the top three levels of the dendrogram
50+
plot_dendrogram(model, truncate_mode='level', p=3)
51+
plt.xlabel("Number of points in node (or index of point if no parenthesis).")
52+
plt.show()
Binary file not shown.

dev/_downloads/scikit-learn-docs.pdf

66.8 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes

0 commit comments

Comments
 (0)