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
|
An unfitted clone of a clusterer set to |
|
Cluster the rows of a record by the correlation between them. |
|
Name of the estimator's cluster-count hyperparameter. |
|
The default clusterer for model selection: k-means with many restarts. |
|
Fit a clone at |
|
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:
- 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
kclusters.- Parameters:
estimator (
BaseEstimator) – A clusterer with a cluster-count hyperparameter.k (
int) – Number of clusters.
- Return type:
- 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
kclusters and return its labels.- Parameters:
estimator (
BaseEstimator) – A clusterer with a cluster-count hyperparameter.X (
ndarray) – Data.k (
int) – Number of clusters.
- Return type:
- 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:
- Returns:
Truewhen itsmetricis"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.
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) / 2as an affinity;"kmeans"clusters the rows of the correlation matrix.- Parameters:
- Return type:
- Returns:
Cluster label of every row.
- Raises:
ValueError – If
methodis 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)