Clustering of points: k-means++, fuzzy k-means, DBSCAN and hierarchical clustering, with cluster validation helpers.
K-means++, fuzzy k-means and DBSCAN are backed by Apache Commons Math. Points are sequences of numbers (or arrays); a plain number is a 1-dimensional point. All points have to have the same, non-zero dimension and finite coordinates.
Every clustering function returns a vector of cluster maps (hclust returns it as :clusters). A cluster map contains:
:representative - the centre of the cluster (centroid for k-means++ and fuzzy k-means, the mean of cluster points for DBSCAN and hierarchical clustering),:size - the number of points,:indices - positions of the cluster points in the input sequence (the order within a cluster is not specified),:data - the cluster points (vectors of doubles), unless :add-data? is false.The order of clusters and the choice of representatives of k-means++, fuzzy k-means and DBSCAN depend on the random number generator. The default :rng is a new generator for every call, pass a seeded generator (fastmath.random/rng) for reproducible results.
Options are validated: an invalid option throws ex-info with the keys :option, :value and :expected. :value is the invalid value; it is nil when the value is the whole input or a whole result (data can be large), and a single point when the points differ in dimension. Unknown option keys are ignored.
:distance is a keyword (:euclidean, :chebyshev, :manhattan, :canberra, :earth-movers), a function of two points (ie. a function from fastmath.distance) or an Apache Commons Math DistanceMeasure (hclust takes a keyword or a function only, or :precomputed for a distance matrix). A distance has to be non-negative and equal to 0 for equal points (d(x, x) = 0): the radius selection of dbscan and silhouette rely on it and return wrong values otherwise.
Hierarchical clustering (hclust) returns a map with the cluster maps (:clusters) and the tree of merges (:tree, a dendrogram in the format of R hclust); cut cuts the tree into another number of clusters, cophenetic, cophenetic-correlation and ->linkage-matrix work on the tree.
Functions: kmeans++, fuzzy-kmeans, dbscan, infer-dbscan-radius, hclust (clustering and the radius selection), predict, wcss, silhouette (working on a clustering result), cut, cophenetic, cophenetic-correlation, ->linkage-matrix (working on a result of hclust).
Clustering of points: k-means++, fuzzy k-means, DBSCAN and hierarchical clustering, with cluster validation helpers. K-means++, fuzzy k-means and DBSCAN are backed by Apache Commons Math. Points are sequences of numbers (or arrays); a plain number is a 1-dimensional point. All points have to have the same, non-zero dimension and finite coordinates. Every clustering function returns a vector of cluster maps ([[hclust]] returns it as `:clusters`). A cluster map contains: - `:representative` - the centre of the cluster (centroid for k-means++ and fuzzy k-means, the mean of cluster points for DBSCAN and hierarchical clustering), - `:size` - the number of points, - `:indices` - positions of the cluster points in the input sequence (the order within a cluster is not specified), - `:data` - the cluster points (vectors of doubles), unless `:add-data?` is `false`. The order of clusters and the choice of representatives of k-means++, fuzzy k-means and DBSCAN depend on the random number generator. The default `:rng` is a new generator for every call, pass a seeded generator (`fastmath.random/rng`) for reproducible results. Options are validated: an invalid option throws `ex-info` with the keys `:option`, `:value` and `:expected`. `:value` is the invalid value; it is `nil` when the value is the whole input or a whole result (data can be large), and a single point when the points differ in dimension. Unknown option keys are ignored. `:distance` is a keyword (`:euclidean`, `:chebyshev`, `:manhattan`, `:canberra`, `:earth-movers`), a function of two points (ie. a function from `fastmath.distance`) or an Apache Commons Math `DistanceMeasure` ([[hclust]] takes a keyword or a function only, or `:precomputed` for a distance matrix). A distance has to be non-negative and equal to 0 for equal points (`d(x, x) = 0`): the radius selection of [[dbscan]] and [[silhouette]] rely on it and return wrong values otherwise. Hierarchical clustering ([[hclust]]) returns a map with the cluster maps (`:clusters`) and the tree of merges (`:tree`, a dendrogram in the format of R `hclust`); [[cut]] cuts the tree into another number of clusters, [[cophenetic]], [[cophenetic-correlation]] and [[->linkage-matrix]] work on the tree. Functions: [[kmeans++]], [[fuzzy-kmeans]], [[dbscan]], [[infer-dbscan-radius]], [[hclust]] (clustering and the radius selection), [[predict]], [[wcss]], [[silhouette]] (working on a clustering result), [[cut]], [[cophenetic]], [[cophenetic-correlation]], [[->linkage-matrix]] (working on a result of [[hclust]]).
(->linkage-matrix result)Converts the tree of a result of hclust to the SciPy linkage matrix.
Parameters:
result - a result of hclustReturns a vector of n-1 rows [a b height count]: clusters a < b merged at height into a cluster of count observations. Clusters 0..n-1 are observations, the cluster made by the row i (counted from 0) is n+i.
Throws ex-info for an invalid result.
See also hclust.
Converts the tree of a result of [[hclust]] to the SciPy linkage matrix. Parameters: - `result` - a result of [[hclust]] Returns a vector of `n-1` rows `[a b height count]`: clusters `a < b` merged at `height` into a cluster of `count` observations. Clusters `0..n-1` are observations, the cluster made by the row `i` (counted from 0) is `n+i`. Throws `ex-info` for an invalid result. See also [[hclust]].
(cophenetic result)Cophenetic distances of a result of hclust: the height of the merge which first joins two observations.
Parameters:
result - a result of hclustReturns a square matrix (a vector of n vectors of doubles) with zeros on the diagonal, as R cophenetic.
Throws ex-info for an invalid result.
See also cophenetic-correlation, hclust.
Cophenetic distances of a result of [[hclust]]: the height of the merge which first joins two observations. Parameters: - `result` - a result of [[hclust]] Returns a square matrix (a vector of `n` vectors of doubles) with zeros on the diagonal, as R `cophenetic`. Throws `ex-info` for an invalid result. See also [[cophenetic-correlation]], [[hclust]].
(cophenetic-correlation result xss)Cophenetic correlation of a result of hclust: how well the tree preserves the distances between observations.
The Pearson correlation between the cophenetic distances (see cophenetic) and the distances of all pairs of observations. A value close to 1 means the dendrogram is a faithful summary of the distances.
Parameters:
Returns a double, NaN for less than 3 observations or when the distances or the cophenetic distances are all equal.
Throws ex-info for an invalid result or xss with another number of observations.
See also cophenetic, hclust.
Cophenetic correlation of a result of [[hclust]]: how well the tree preserves the distances between observations. The Pearson correlation between the cophenetic distances (see [[cophenetic]]) and the distances of all pairs of observations. A value close to 1 means the dendrogram is a faithful summary of the distances. Parameters: - `result` - a result of [[hclust]] - `xss` - the data clustered by [[hclust]] (points or the distance matrix) Returns a double, `NaN` for less than 3 observations or when the distances or the cophenetic distances are all equal. Throws `ex-info` for an invalid result or `xss` with another number of observations. See also [[cophenetic]], [[hclust]].
(cut result xss)(cut result xss {:keys [k h add-data?]})Cuts the tree of a result of hclust into another number of clusters, without clustering again.
Parameters:
result - a result of hclustxss - the data clustered by hclust (points or the distance matrix), needed for :representative, :medoid and :data:k - the number of clusters, an integer between 1 and the number of observations, default: 1:h - the height of the cut, a finite number not smaller than 0 (not for a tree with inversions):add-data? - add :data to the clusters, default: trueReturns a new result, as hclust, with the same :tree.
Throws ex-info for an invalid result, options, or xss with another number of observations.
See also hclust.
Cuts the tree of a result of [[hclust]] into another number of clusters, without clustering again. Parameters: - `result` - a result of [[hclust]] - `xss` - the data clustered by [[hclust]] (points or the distance matrix), needed for `:representative`, `:medoid` and `:data` - options (a map): - `:k` - the number of clusters, an integer between 1 and the number of observations, default: `1` - `:h` - the height of the cut, a finite number not smaller than 0 (not for a tree with inversions) - `:add-data?` - add `:data` to the clusters, default: `true` Returns a new result, as [[hclust]], with the same `:tree`. Throws `ex-info` for an invalid result, options, or `xss` with another number of observations. See also [[hclust]].
(dbscan xss)(dbscan xss
{:keys [eps eps-method points distance add-data? noise?]
:or
{eps-method :knee distance :euclidean add-data? true noise? false}})DBSCAN density-based clustering: points with at least :points neighbours within :eps start (and extend) clusters, other points are noise.
Parameters:
xss - a non-empty sequence of points (sequences, arrays or numbers), see the namespace documentation:eps - the radius of a neighbourhood (inclusive), a finite number greater than 0 or nil to select it, default: nil:eps-method - how to select :eps when it is nil: :knee (default) or :pairs, see infer-dbscan-radius. :pairs uses a new default generator, so it is not reproducible: call infer-dbscan-radius with :rng and pass the result as :eps:points - the number of neighbours (the point itself is not counted) required in the neighbourhood of a core point, an integer at least 1, default: twice the dimension:distance - distance (keyword, function or DistanceMeasure), default: :euclidean:add-data? - add :data to the clusters, default: true:noise? - add the noise points as the last map, default: falseReturns a vector of cluster maps (:representative, :size, :indices and :data), see the namespace documentation. The :representative is the mean of the points of a cluster. With :noise? the last element is {:noise? true :size :indices} (and :data), it does not have :representative and it is present even when there is no noise. Cluster and noise indices are disjoint and cover all input points.
Without :eps the radius is inferred, which needs at least 3 points and throws when no knee is found. The number of neighbours of the Scikit-learn min_samples is :points plus one.
Throws ex-info for an invalid option or data.
See also kmeans++, infer-dbscan-radius, predict, wcss, silhouette.
DBSCAN density-based clustering: points with at least `:points` neighbours within `:eps` start (and extend) clusters, other points are noise.
Parameters:
- `xss` - a non-empty sequence of points (sequences, arrays or numbers), see the namespace documentation
- options (a map):
- `:eps` - the radius of a neighbourhood (inclusive), a finite number greater than 0 or `nil` to select it, default: `nil`
- `:eps-method` - how to select `:eps` when it is `nil`: `:knee` (default) or `:pairs`, see [[infer-dbscan-radius]]. `:pairs` uses a new default generator, so it is not reproducible: call [[infer-dbscan-radius]] with `:rng` and pass the result as `:eps`
- `:points` - the number of neighbours (the point itself is not counted) required in the neighbourhood of a core point, an integer at least 1, default: twice the dimension
- `:distance` - distance (keyword, function or `DistanceMeasure`), default: `:euclidean`
- `:add-data?` - add `:data` to the clusters, default: `true`
- `:noise?` - add the noise points as the last map, default: `false`
Returns a vector of cluster maps (`:representative`, `:size`, `:indices` and `:data`), see the namespace documentation. The `:representative` is the mean of the points of a cluster. With `:noise?` the last element is `{:noise? true :size :indices}` (and `:data`), it does not have `:representative` and it is present even when there is no noise. Cluster and noise indices are disjoint and cover all input points.
Without `:eps` the radius is inferred, which needs at least 3 points and throws when no knee is found. The number of neighbours of the Scikit-learn `min_samples` is `:points` plus one.
Throws `ex-info` for an invalid option or data.
See also [[kmeans++]], [[infer-dbscan-radius]], [[predict]], [[wcss]], [[silhouette]].(fuzzy-kmeans xss)(fuzzy-kmeans xss
{:keys [clusters fuzziness max-iters distance epsilon rng
add-data? add-memberships?]
:or {clusters 1
fuzziness 2.0
max-iters -1
distance :euclidean
epsilon 0.001
rng (r/rng :default)
add-data? true
add-memberships? false}})Fuzzy k-means clustering: every point has a degree of membership in every cluster, points are grouped by their highest membership.
Parameters:
xss - a non-empty sequence of points (sequences, arrays or numbers), see the namespace documentation:clusters - the number of clusters, an integer between 1 and the number of points, default: 1:fuzziness - the fuzziness coefficient, a finite number greater than 1, default: 2.0:max-iters - the maximum number of iterations, a negative value means no limit, default: -1:epsilon - convergence threshold (a change of the membership matrix), a finite number greater than 0, default: 1.0e-3:distance - distance (keyword, function or DistanceMeasure), default: :euclidean:rng - random number generator, default: a new default generator for every call (seed it for reproducible results):add-data? - add :data to the clusters, default: true:add-memberships? - add :memberships to the clusters, default: falseReturns a vector of cluster maps (:representative, :size, :indices and :data), see the namespace documentation. :size and :data describe the hard assignment (the highest membership). With :add-memberships?, the cluster map contains :memberships, the vector with the membership of every input point in the cluster (in the order of input). The vector has the metadata :objective, the value of the objective function.
A cluster that gets no point (eg. all points are equal and there is more than one cluster) has :size 0, empty :indices and :data, memberships 0.0 and the representative [##NaN ##NaN ...]: its centre is a weighted mean with zero weights. Check :size before using the representatives of such a result. predict, wcss and silhouette throw ex-info for such a result.
Throws ex-info for an invalid option or data.
See also kmeans++, dbscan, predict, wcss, silhouette.
Fuzzy k-means clustering: every point has a degree of membership in every cluster, points are grouped by their highest membership. Parameters: - `xss` - a non-empty sequence of points (sequences, arrays or numbers), see the namespace documentation - options (a map): - `:clusters` - the number of clusters, an integer between 1 and the number of points, default: `1` - `:fuzziness` - the fuzziness coefficient, a finite number greater than 1, default: `2.0` - `:max-iters` - the maximum number of iterations, a negative value means no limit, default: `-1` - `:epsilon` - convergence threshold (a change of the membership matrix), a finite number greater than 0, default: `1.0e-3` - `:distance` - distance (keyword, function or `DistanceMeasure`), default: `:euclidean` - `:rng` - random number generator, default: a new default generator for every call (seed it for reproducible results) - `:add-data?` - add `:data` to the clusters, default: `true` - `:add-memberships?` - add `:memberships` to the clusters, default: `false` Returns a vector of cluster maps (`:representative`, `:size`, `:indices` and `:data`), see the namespace documentation. `:size` and `:data` describe the hard assignment (the highest membership). With `:add-memberships?`, the cluster map contains `:memberships`, the vector with the membership of every input point in the cluster (in the order of input). The vector has the metadata `:objective`, the value of the objective function. A cluster that gets no point (eg. all points are equal and there is more than one cluster) has `:size` `0`, empty `:indices` and `:data`, memberships `0.0` and the representative `[##NaN ##NaN ...]`: its centre is a weighted mean with zero weights. Check `:size` before using the representatives of such a result. [[predict]], [[wcss]] and [[silhouette]] throw `ex-info` for such a result. Throws `ex-info` for an invalid option or data. See also [[kmeans++]], [[dbscan]], [[predict]], [[wcss]], [[silhouette]].
(hclust xss)(hclust xss {:keys [method distance algorithm k h add-data?]})Hierarchical clustering: builds a tree of nested clusters (a dendrogram) and cuts it into flat clusters.
Agglomerative methods start with every observation as a cluster and repeatedly merge the two closest clusters, the distance between clusters is defined by the linkage :method. The divisive method (:diana, DIANA) starts with one cluster and splits clusters until every observation is alone: the most distant observation starts a splinter group and observations closer to it on average move to it; the height of a split is the diameter of the split cluster. The result follows R hclust: the merge heights and the encoding of merges are the same.
Parameters:
xss - a non-empty sequence of points (sequences, arrays or numbers, see the namespace documentation) or, with :distance :precomputed, a square matrix (a sequence or an array of rows) of distances between observations: finite, non-negative, symmetric up to rounding, with zeros on the diagonal (only the lower triangle is used). At most 65536 observations.:method - the linkage, default: :complete
:single - the shortest distance between points of two clusters:complete - the longest distance between points of two clusters:average - the mean distance between points of two clusters (UPGMA):weighted - the mean of distances of the two merged clusters (WPGMA, R mcquitty):centroid - the distance between centroids (UPGMC), as SciPy centroid (R centroid on squared distances, the heights are square roots):median - the distance between weighted centroids (WPGMC), as SciPy median (R median on squared distances, the heights are square roots):ward - the increase of the within-cluster variance, Ward's method on squared distances (R ward.D2, SciPy ward):ward-d - Ward's update applied to distances, not squared (R ward.D):diana - divisive analysis (R cluster::diana):distance - a keyword (:euclidean, :chebyshev, :manhattan, :canberra, :earth-movers), a function of two points (ie. a function from fastmath.distance, it gets double arrays) or :precomputed for a distance matrix, default: :euclidean. A function has to return finite, non-negative numbers.:algorithm - :generic (default) or :fast (agglomerative methods only, it throws for :diana), see below:k - the number of clusters, an integer between 1 and the number of observations, default: 1:h - the height of the cut, a finite number not smaller than 0: merges at a height not greater than :h are kept. Only one of :k and :h can be given.:add-data? - add :data to the clusters, default: trueAny distance works with any method. The :centroid, :median and :ward methods assume the Euclidean distance: with another distance the heights have no geometric meaning, and :centroid or :median can give NaN heights (a negative squared distance).
:algorithm :generic reproduces R hclust exactly, also for ties. :fast uses the minimum spanning tree (:single) or the nearest-neighbour chain (:complete, :average, :weighted, :ward, :ward-d), as SciPy, and is faster for many observations. Without ties the tree is the same and the heights agree up to rounding; with ties another tree with the same heights can be returned. :centroid and :median always use the generic algorithm.
Returns a map:
:clusters - a vector of cluster maps, ordered by the first observation of every cluster (as R cutree):tree - the tree of merges (a dendrogram)A cluster map contains:
:indices - positions of the observations in xss, ascending:size - the number of observations:medoid - the position of the observation with the lowest sum of distances to the other observations of the cluster (the lowest position on ties):representative - the mean of the points of the cluster (only for points):data - the points of the cluster (only for points, unless :add-data? is false)The tree is a map:
:merge - n-1 merges [a b] as in R hclust: a negative number -i is the observation at the position i-1 of xss, a positive number j is the cluster made by the merge j (counted from 1). A single observation goes first, two clusters are ordered.:height - n-1 heights of the merges, non-decreasing except for :centroid and :median (inversions):order - positions of the observations in the order of the leaves of the dendrogram (for plotting):n - the number of observations:method, :distance, :algorithm - the options used (no :algorithm for :diana):dc - the divisive coefficient (:diana only, NaN for one observation or identical observations)The :clusters of a result of points work with predict, wcss and silhouette; for a distance matrix they have no :representative and :data, so these functions throw.
It needs O(n^2) memory (two arrays of the n(n-1)/2 distances for agglomerative methods), the time is between O(n^2) and O(n^3).
Throws ex-info for an invalid option or data, for a :distance function returning a value which is not a finite, non-negative number, for :h with inversions, and for :algorithm with :diana.
See also cut, cophenetic, cophenetic-correlation, ->linkage-matrix.
Hierarchical clustering: builds a tree of nested clusters (a dendrogram) and cuts it into flat clusters.
Agglomerative methods start with every observation as a cluster and repeatedly merge the two closest clusters, the distance between clusters is defined by the linkage `:method`. The divisive method (`:diana`, DIANA) starts with one cluster and splits clusters until every observation is alone: the most distant observation starts a splinter group and observations closer to it on average move to it; the height of a split is the diameter of the split cluster. The result follows R `hclust`: the merge heights and the encoding of merges are the same.
Parameters:
- `xss` - a non-empty sequence of points (sequences, arrays or numbers, see the namespace documentation) or, with `:distance` `:precomputed`, a square matrix (a sequence or an array of rows) of distances between observations: finite, non-negative, symmetric up to rounding, with zeros on the diagonal (only the lower triangle is used). At most 65536 observations.
- options (a map):
- `:method` - the linkage, default: `:complete`
- `:single` - the shortest distance between points of two clusters
- `:complete` - the longest distance between points of two clusters
- `:average` - the mean distance between points of two clusters (UPGMA)
- `:weighted` - the mean of distances of the two merged clusters (WPGMA, R `mcquitty`)
- `:centroid` - the distance between centroids (UPGMC), as SciPy `centroid` (R `centroid` on squared distances, the heights are square roots)
- `:median` - the distance between weighted centroids (WPGMC), as SciPy `median` (R `median` on squared distances, the heights are square roots)
- `:ward` - the increase of the within-cluster variance, Ward's method on squared distances (R `ward.D2`, SciPy `ward`)
- `:ward-d` - Ward's update applied to distances, not squared (R `ward.D`)
- `:diana` - divisive analysis (R `cluster::diana`)
- `:distance` - a keyword (`:euclidean`, `:chebyshev`, `:manhattan`, `:canberra`, `:earth-movers`), a function of two points (ie. a function from `fastmath.distance`, it gets double arrays) or `:precomputed` for a distance matrix, default: `:euclidean`. A function has to return finite, non-negative numbers.
- `:algorithm` - `:generic` (default) or `:fast` (agglomerative methods only, it throws for `:diana`), see below
- `:k` - the number of clusters, an integer between 1 and the number of observations, default: `1`
- `:h` - the height of the cut, a finite number not smaller than 0: merges at a height not greater than `:h` are kept. Only one of `:k` and `:h` can be given.
- `:add-data?` - add `:data` to the clusters, default: `true`
Any distance works with any method. The `:centroid`, `:median` and `:ward` methods assume the Euclidean distance: with another distance the heights have no geometric meaning, and `:centroid` or `:median` can give `NaN` heights (a negative squared distance).
`:algorithm` `:generic` reproduces R `hclust` exactly, also for ties. `:fast` uses the minimum spanning tree (`:single`) or the nearest-neighbour chain (`:complete`, `:average`, `:weighted`, `:ward`, `:ward-d`), as SciPy, and is faster for many observations. Without ties the tree is the same and the heights agree up to rounding; with ties another tree with the same heights can be returned. `:centroid` and `:median` always use the generic algorithm.
Returns a map:
- `:clusters` - a vector of cluster maps, ordered by the first observation of every cluster (as R `cutree`)
- `:tree` - the tree of merges (a dendrogram)
A cluster map contains:
- `:indices` - positions of the observations in `xss`, ascending
- `:size` - the number of observations
- `:medoid` - the position of the observation with the lowest sum of distances to the other observations of the cluster (the lowest position on ties)
- `:representative` - the mean of the points of the cluster (only for points)
- `:data` - the points of the cluster (only for points, unless `:add-data?` is `false`)
The tree is a map:
- `:merge` - `n-1` merges `[a b]` as in R `hclust`: a negative number `-i` is the observation at the position `i-1` of `xss`, a positive number `j` is the cluster made by the merge `j` (counted from 1). A single observation goes first, two clusters are ordered.
- `:height` - `n-1` heights of the merges, non-decreasing except for `:centroid` and `:median` (inversions)
- `:order` - positions of the observations in the order of the leaves of the dendrogram (for plotting)
- `:n` - the number of observations
- `:method`, `:distance`, `:algorithm` - the options used (no `:algorithm` for `:diana`)
- `:dc` - the divisive coefficient (`:diana` only, `NaN` for one observation or identical observations)
The `:clusters` of a result of points work with [[predict]], [[wcss]] and [[silhouette]]; for a distance matrix they have no `:representative` and `:data`, so these functions throw.
It needs O(n^2) memory (two arrays of the `n(n-1)/2` distances for agglomerative methods), the time is between O(n^2) and O(n^3).
Throws `ex-info` for an invalid option or data, for a `:distance` function returning a value which is not a finite, non-negative number, for `:h` with inversions, and for `:algorithm` with `:diana`.
See also [[cut]], [[cophenetic]], [[cophenetic-correlation]], [[->linkage-matrix]].(infer-dbscan-radius xss dist)(infer-dbscan-radius xss
dist
{:keys [method points sensitivity rng]
:or {method :knee sensitivity 1.0 rng (r/rng :default)}})Infers the :eps radius for dbscan from data.
Parameters:
xss - a sequence of at least 3 points, see the namespace documentationdist - distance (keyword, function or DistanceMeasure):method - :knee (default) or :pairs:points - the number of neighbours (as :points in dbscan), an integer at least 1, default: twice the dimension:sensitivity - sensitivity of the knee detection, a finite number not smaller than 0, default: 1.0 (:knee only):rng - random number generator (:pairs only), default: a new default generator for every callThe :knee method sorts the distances from every point to its :points-th nearest neighbour and returns the distance at the knee of this curve (the Kneedle algorithm, Satopaa et al. 2011, for an increasing convex curve). It needs O(n^2) time and O(n) memory. It throws ex-info when no knee is found (eg. for uniform data), use an explicit :eps then. The distance has to be 0 for equal points.
The :pairs method is a heuristic: kmeans++ with two clusters on all pairwise distances, the lower centre is the radius. It needs O(n^2) memory and depends on the generator. It throws ex-info when all distances between points are equal (eg. identical points).
Returns the radius as a double. Throws ex-info for an invalid option or data.
See also dbscan.
Infers the `:eps` radius for [[dbscan]] from data. Parameters: - `xss` - a sequence of at least 3 points, see the namespace documentation - `dist` - distance (keyword, function or `DistanceMeasure`) - options (a map): - `:method` - `:knee` (default) or `:pairs` - `:points` - the number of neighbours (as `:points` in [[dbscan]]), an integer at least 1, default: twice the dimension - `:sensitivity` - sensitivity of the knee detection, a finite number not smaller than 0, default: `1.0` (`:knee` only) - `:rng` - random number generator (`:pairs` only), default: a new default generator for every call The `:knee` method sorts the distances from every point to its `:points`-th nearest neighbour and returns the distance at the knee of this curve (the Kneedle algorithm, Satopaa et al. 2011, for an increasing convex curve). It needs O(n^2) time and O(n) memory. It throws `ex-info` when no knee is found (eg. for uniform data), use an explicit `:eps` then. The distance has to be 0 for equal points. The `:pairs` method is a heuristic: kmeans++ with two clusters on all pairwise distances, the lower centre is the radius. It needs O(n^2) memory and depends on the generator. It throws `ex-info` when all distances between points are equal (eg. identical points). Returns the radius as a double. Throws `ex-info` for an invalid option or data. See also [[dbscan]].
(kmeans++ xss)(kmeans++ xss
{:keys [clusters max-iters distance rng empty-cluster-strategy trials
add-data?]
:or {clusters 1
max-iters -1
distance :euclidean
empty-cluster-strategy :largest-variance
trials 1
rng (r/rng :default)
add-data? true}})K-means++ clustering: partitions points into :clusters groups minimising the within-cluster variance.
Parameters:
xss - a non-empty sequence of points (sequences, arrays or numbers), see the namespace documentation:clusters - the number of clusters, an integer between 1 and the number of distinct points, default: 1:max-iters - the maximum number of iterations, a negative value means no limit, default: -1. A distance that is 0 for different points (eg. a constant function) can keep the algorithm from converging, set :max-iters then:distance - distance (keyword, function or DistanceMeasure), default: :euclidean:rng - random number generator, default: a new default generator for every call (seed it for reproducible results):empty-cluster-strategy - what to do with an empty cluster: :largest-variance (default), :largest-points-number, :farthest-point or :error:trials - the number of runs, the best (the lowest sum of cluster variances) is returned, default: 1:add-data? - add :data to the clusters, default: trueReturns a vector of cluster maps (:representative, :size, :indices and :data), see the namespace documentation. The order of clusters depends on the generator.
Throws ex-info for an invalid option or data, also when :clusters is greater than the number of distinct points. With :empty-cluster-strategy set to :error an empty cluster throws an exception.
See also fuzzy-kmeans, dbscan, predict, wcss, silhouette.
K-means++ clustering: partitions points into `:clusters` groups minimising the within-cluster variance. Parameters: - `xss` - a non-empty sequence of points (sequences, arrays or numbers), see the namespace documentation - options (a map): - `:clusters` - the number of clusters, an integer between 1 and the number of distinct points, default: `1` - `:max-iters` - the maximum number of iterations, a negative value means no limit, default: `-1`. A distance that is 0 for different points (eg. a constant function) can keep the algorithm from converging, set `:max-iters` then - `:distance` - distance (keyword, function or `DistanceMeasure`), default: `:euclidean` - `:rng` - random number generator, default: a new default generator for every call (seed it for reproducible results) - `:empty-cluster-strategy` - what to do with an empty cluster: `:largest-variance` (default), `:largest-points-number`, `:farthest-point` or `:error` - `:trials` - the number of runs, the best (the lowest sum of cluster variances) is returned, default: `1` - `:add-data?` - add `:data` to the clusters, default: `true` Returns a vector of cluster maps (`:representative`, `:size`, `:indices` and `:data`), see the namespace documentation. The order of clusters depends on the generator. Throws `ex-info` for an invalid option or data, also when `:clusters` is greater than the number of distinct points. With `:empty-cluster-strategy` set to `:error` an empty cluster throws an exception. See also [[fuzzy-kmeans]], [[dbscan]], [[predict]], [[wcss]], [[silhouette]].
(predict result x)(predict result x distance)Finds the cluster of a point: the index of the nearest representative.
Parameters:
result - a result of kmeans++, fuzzy-kmeans, dbscan or the :clusters of hclust on points (the noise map is ignored)x - a point of the dimension of the representativesdistance - distance (keyword, function or DistanceMeasure), default: :euclidean; use the one used for the clusteringReturns the position of the cluster in result (ties go to the first cluster). It does not return noise: for dbscan the representatives are means of the clusters, so the result is meaningful for compact (convex) clusters only.
Throws ex-info for an invalid result or a point of another dimension.
See also wcss, silhouette.
Finds the cluster of a point: the index of the nearest representative. Parameters: - `result` - a result of [[kmeans++]], [[fuzzy-kmeans]], [[dbscan]] or the `:clusters` of [[hclust]] on points (the noise map is ignored) - `x` - a point of the dimension of the representatives - `distance` - distance (keyword, function or `DistanceMeasure`), default: `:euclidean`; use the one used for the clustering Returns the position of the cluster in `result` (ties go to the first cluster). It does not return noise: for [[dbscan]] the representatives are means of the clusters, so the result is meaningful for compact (convex) clusters only. Throws `ex-info` for an invalid result or a point of another dimension. See also [[wcss]], [[silhouette]].
(silhouette result)(silhouette result distance)Silhouette of a clustering: how well points fit their cluster compared to the nearest other cluster.
For a point, a is the mean distance to other points of its cluster and b is the lowest mean distance to the points of another cluster, the silhouette is (b - a) / max(a, b). It is in the range from -1 to 1 (higher is better), 0 for a point in a one-point cluster.
Parameters:
result - a result of kmeans++, fuzzy-kmeans, dbscan or the :clusters of hclust on points with :data and at least 2 clusters (the noise map is ignored)distance - distance (keyword, function or DistanceMeasure), default: :euclideanReturns a map:
:mean - the mean silhouette of all clustered points:samples - silhouettes of clustered points, ordered by position in the input:indices - positions of the points in :samples in the input sequence (noise points are not included)It needs O(n^2) time and O(n) memory.
Throws ex-info for an invalid result, a result without :data (:add-data? set to false), with less than 2 clusters or with an empty cluster (see fuzzy-kmeans). The distance has to be 0 for equal points.
Silhouette of a clustering: how well points fit their cluster compared to the nearest other cluster. For a point, `a` is the mean distance to other points of its cluster and `b` is the lowest mean distance to the points of another cluster, the silhouette is `(b - a) / max(a, b)`. It is in the range from -1 to 1 (higher is better), `0` for a point in a one-point cluster. Parameters: - `result` - a result of [[kmeans++]], [[fuzzy-kmeans]], [[dbscan]] or the `:clusters` of [[hclust]] on points with `:data` and at least 2 clusters (the noise map is ignored) - `distance` - distance (keyword, function or `DistanceMeasure`), default: `:euclidean` Returns a map: - `:mean` - the mean silhouette of all clustered points - `:samples` - silhouettes of clustered points, ordered by position in the input - `:indices` - positions of the points in `:samples` in the input sequence (noise points are not included) It needs O(n^2) time and O(n) memory. Throws `ex-info` for an invalid result, a result without `:data` (`:add-data?` set to `false`), with less than 2 clusters or with an empty cluster (see [[fuzzy-kmeans]]). The distance has to be 0 for equal points. See also [[wcss]], [[predict]].
(wcss result)(wcss result distance)Within-cluster sum of squares: the sum over all points of the squared distance to the representative of the cluster.
Parameters:
result - a result of kmeans++, fuzzy-kmeans, dbscan or the :clusters of hclust on points with :data (the noise map is ignored)distance - distance (keyword, function or DistanceMeasure), default: :euclideanReturns a double, lower is better for the same number of clusters.
Throws ex-info for an invalid result or a result without :data (:add-data? set to false).
See also silhouette, predict.
Within-cluster sum of squares: the sum over all points of the squared distance to the representative of the cluster. Parameters: - `result` - a result of [[kmeans++]], [[fuzzy-kmeans]], [[dbscan]] or the `:clusters` of [[hclust]] on points with `:data` (the noise map is ignored) - `distance` - distance (keyword, function or `DistanceMeasure`), default: `:euclidean` Returns a double, lower is better for the same number of clusters. Throws `ex-info` for an invalid result or a result without `:data` (`:add-data?` set to `false`). See also [[silhouette]], [[predict]].
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |