milia.cluster.fitting#

Refitting a clusterer at a chosen number of clusters.

Model-selection code sweeps k by cloning one clusterer and setting its cluster count. milia’s clusterers all call it n_clusters; scikit-learn’s Gaussian mixture calls it n_components, so both are recognised.

Functions

at(estimator, k)

An unfitted clone of a clusterer set to k clusters.

bycorrelation(series[, k, method, random_state])

Cluster the rows of a record by the correlation between them.

countparam(estimator)

Name of the estimator's cluster-count hyperparameter.

default([random_state])

The default clusterer for model selection: k-means with many restarts.

labels(estimator, X, k)

Fit a clone at k clusters and return its labels.

precomputed(estimator)

Whether the estimator expects a square distance matrix.

milia.cluster.fitting.countparam(estimator)#

Name of the estimator’s cluster-count hyperparameter.

Parameters:

estimator (BaseEstimator) – A clusterer.

Return type:

str

Returns:

"n_clusters" or "n_components".

Raises:

TypeError – If the estimator has neither, as for a density clusterer.

Examples

>>> from sklearn.mixture import GaussianMixture
>>> countparam(GaussianMixture())
'n_components'
milia.cluster.fitting.at(estimator, k)#

An unfitted clone of a clusterer set to k clusters.

Parameters:
  • estimator (BaseEstimator) – A clusterer with a cluster-count hyperparameter.

  • k (int) – Number of clusters.

Return type:

BaseEstimator

Returns:

The clone.

Examples

>>> from milia import make
>>> at(make("clusterer", "kmeans"), 5).n_clusters
5
milia.cluster.fitting.labels(estimator, X, k)#

Fit a clone at k clusters and return its labels.

Parameters:
  • estimator (BaseEstimator) – A clusterer with a cluster-count hyperparameter.

  • X (ndarray) – Data.

  • k (int) – Number of clusters.

Return type:

ndarray

Returns:

The labels.

Examples

>>> import numpy as np
>>> from milia import make
>>> labels(make("clusterer", "kmeans"), np.array([[0.0], [0.1], [5.0]]), 2).tolist()
[0, 0, 1]
milia.cluster.fitting.precomputed(estimator)#

Whether the estimator expects a square distance matrix.

Parameters:

estimator (BaseEstimator) – A clusterer.

Return type:

bool

Returns:

True when its metric is "precomputed".

Examples

>>> from milia import make
>>> precomputed(make("clusterer", "agglomerative", linkage="average", metric="precomputed"))
True
milia.cluster.fitting.default(random_state=0)#

The default clusterer for model selection: k-means with many restarts.

Parameters:

random_state (int) – Seed.

Return type:

BaseEstimator

Returns:

An unfitted KMeans.

Examples

>>> default().n_init
20
milia.cluster.fitting.bycorrelation(series, k=3, method='spectral', random_state=0)#

Cluster the rows of a record by the correlation between them.

The correlation of two series ignores their level and scale and keeps their co-movement, so rows driven by a common source fall together. "spectral" treats (1 + correlation) / 2 as an affinity; "kmeans" clusters the rows of the correlation matrix.

Parameters:
  • series (ndarray) – Array of shape (rows, steps).

  • k (int) – Number of clusters.

  • method (str) – "spectral" or "kmeans".

  • random_state (int) – Seed.

Return type:

ndarray

Returns:

Cluster label of every row.

Raises:

ValueError – If method is neither "spectral" nor "kmeans".

Examples

>>> rng = np.random.default_rng(0)
>>> drivers = rng.normal(size=(2, 200))
>>> rows = np.vstack(
...     [drivers[0] + 0.1 * rng.normal(size=(4, 200)), drivers[1] + 0.1 * rng.normal(size=(4, 200))]
... )
>>> labels = bycorrelation(rows, k=2)
>>> len(set(labels[:4])), len(set(labels[4:])), bool(labels[0] != labels[4])
(1, 1, True)