| 1 |
fastcluster: Fast hierarchical clustering routines for R and Python |
| 2 |
|
| 3 |
Copyright: |
| 4 |
* Until package version 1.1.23: © 2011 Daniel Müllner <https://danifold.net> |
| 5 |
* All changes from version 1.1.24 on: © Google Inc. <https://www.google.com> |
| 6 |
|
| 7 |
The fastcluster package is a C++ library for hierarchical, agglomerative |
| 8 |
clustering. It efficiently implements the seven most widely used clustering |
| 9 |
schemes: single, complete, average, weighted/McQuitty, Ward, centroid and |
| 10 |
median linkage. The library currently has interfaces to two languages: R and |
| 11 |
Python/NumPy. Part of the functionality is designed as drop-in replacement for |
| 12 |
existing routines: “linkage” in the SciPy package “scipy.cluster.hierarchy”, |
| 13 |
“hclust” in R's “stats” package, and the “flashClust” package. Once the |
| 14 |
fastcluster library is loaded at the beginning of the code, every program that |
| 15 |
uses hierarchical clustering can benefit immediately and effortlessly from the |
| 16 |
performance gain. Moreover, there are memory-saving routines for clustering of |
| 17 |
vector data, which go beyond what the existing packages provide. |
| 18 |
|
| 19 |
See the author's home page <https://danifold.net> for more |
| 20 |
information, in particular a performance comparison with other clustering |
| 21 |
packages. The User's manual is the file inst/doc/fastcluster.pdf in the |
| 22 |
source distribution. |
| 23 |
|
| 24 |
The fastcluster package is distributed under the BSD license. See the file |
| 25 |
LICENSE in the source distribution or |
| 26 |
<http://opensource.org/licenses/BSD-2-Clause>. |
| 27 |
|
| 28 |
Christoph Dalitz wrote a pure C++ interface to fastcluster: |
| 29 |
<https://lionel.kr.hs-niederrhein.de/~dalitz/data/hclust/>. |
| 30 |
|
| 31 |
|
| 32 |
Installation |
| 33 |
‾‾‾‾‾‾‾‾‾‾‾‾ |
| 34 |
See the file INSTALL in the source distribution. |
| 35 |
|
| 36 |
|
| 37 |
Usage |
| 38 |
‾‾‾‾‾ |
| 39 |
1. R |
| 40 |
‾‾‾‾ |
| 41 |
In R, load the package with the following command: |
| 42 |
|
| 43 |
library('fastcluster') |
| 44 |
|
| 45 |
The package overwrites the function hclust from the “stats” package (in the |
| 46 |
same way as the flashClust package does). Please remove any references to the |
| 47 |
flashClust package in your R files to not accidentally overwrite the hclust |
| 48 |
function with the flashClust version. |
| 49 |
|
| 50 |
The new hclust function has exactly the same calling conventions as the old |
| 51 |
one. You may just load the package and immediately and effortlessly enjoy the |
| 52 |
performance improvements. The function is also an improvement to the flashClust |
| 53 |
function from the “flashClust” package. Just replace every call to flashClust |
| 54 |
by hclust and expect your code to work as before, only faster. (If you are |
| 55 |
using flashClust prior to version 1.01, update it! See the change log for |
| 56 |
flashClust: |
| 57 |
|
| 58 |
http://cran.r-project.org/web/packages/flashClust/ChangeLog ) |
| 59 |
|
| 60 |
If you need to access the old function or make sure that the right function is |
| 61 |
called, specify the package as follows: |
| 62 |
|
| 63 |
fastcluster::hclust(…) |
| 64 |
flashClust::hclust(…) |
| 65 |
stats::hclust(…) |
| 66 |
|
| 67 |
Vector data can be clustered with a memory-saving algorithm with the command |
| 68 |
|
| 69 |
hclust.vector(…) |
| 70 |
|
| 71 |
See the User's manual inst/doc/fastcluster.pdf for further details. |
| 72 |
|
| 73 |
WARNING |
| 74 |
‾‾‾‾‾‾‾ |
| 75 |
R and Matlab/SciPy use different conventions for the “Ward”, “centroid” and |
| 76 |
“median” methods. R assumes that the dissimilarity matrix consists of squared |
| 77 |
Euclidean distances, while Matlab and SciPy expect non-squared Euclidean |
| 78 |
distances. The fastcluster package respects these conventions and uses |
| 79 |
different formulas in the two interfaces. |
| 80 |
|
| 81 |
If you want the same results in both interfaces, then feed the hclust function |
| 82 |
in R with the entry-wise square of the distance matrix, D^2, for the “Ward”, |
| 83 |
“centroid” and “median” methods and later take the square root of the height |
| 84 |
field in the dendrogram. For the “average” and “weighted” alias “mcquitty” |
| 85 |
methods, you must still take the same distance matrix D as in the Python |
| 86 |
interface for the same results. The “single” and “complete” methods only depend |
| 87 |
on the relative order of the distances, hence it does not make a difference |
| 88 |
whether the method operates on the distances or the squared distances. |
| 89 |
|
| 90 |
The code example in the R documentation (enter ?hclust or example(hclust) in R) |
| 91 |
contains an instance where the squared distance matrix is generated from |
| 92 |
Euclidean data. |
| 93 |
|
| 94 |
2. Python |
| 95 |
‾‾‾‾‾‾‾‾‾ |
| 96 |
The fastcluster package is imported as usual by |
| 97 |
|
| 98 |
import fastcluster |
| 99 |
|
| 100 |
It provides the following functions: |
| 101 |
|
| 102 |
linkage(X, method='single', metric='euclidean', preserve_input=True) |
| 103 |
single(X) |
| 104 |
complete(X) |
| 105 |
average(X) |
| 106 |
weighted(X) |
| 107 |
ward(X) |
| 108 |
centroid(X) |
| 109 |
median(X) |
| 110 |
linkage_vector(X, method='single', metric='euclidean', extraarg=None) |
| 111 |
|
| 112 |
The argument X is either a compressed distance matrix or a collection of n |
| 113 |
observation vectors in d dimensions as an (n×d) array. Apart from the argument |
| 114 |
preserve_input, the methods have the same input and output as the functions of |
| 115 |
the same name in the package scipy.cluster.hierarchy. |
| 116 |
|
| 117 |
The additional, optional argument preserve_input specifies whether the |
| 118 |
fastcluster package first copies the distance matrix or writes into the |
| 119 |
existing array. If the dissimilarities are generated for the clustering step |
| 120 |
only and are not needed afterward, approximately half the memory can be saved |
| 121 |
by specifying preserve_input=False. Note that the input array X contains |
| 122 |
unspecified values after this procedure. You may want to write |
| 123 |
|
| 124 |
linkage(X, method='…', preserve_input=False) |
| 125 |
del X |
| 126 |
|
| 127 |
to make sure that the matrix X is not accessed accidentally after it has been |
| 128 |
used as scratch memory. |
| 129 |
|
| 130 |
The method |
| 131 |
|
| 132 |
linkage_vector(X, method='single', metric='euclidean', extraarg=None) |
| 133 |
|
| 134 |
provides memory-saving clustering for vector data. It also accepts a collection |
| 135 |
of n observation vectors in d dimensions as an (n×d) array as the first parameter. |
| 136 |
The parameter 'method' is either 'single', 'ward', 'centroid' or 'median'. The |
| 137 |
'ward', 'centroid' and 'median' methods require the Euclidean metric. In case |
| 138 |
of single linkage, the 'metric' parameter can be chosen from all metrics which |
| 139 |
are implemented in scipy.spatial.dist.pdist. There may be differences between |
| 140 |
|
| 141 |
linkage(scipy.spatial.dist.pdist(X, metric='…')) |
| 142 |
and |
| 143 |
linkage_vector(X, metric='…') |
| 144 |
|
| 145 |
since there have been made a few corrections compared to the pdist function. |
| 146 |
Please consult the the User's manual inst/doc/fastcluster.pdf for |
| 147 |
comprehensive details. |