milia.represent.crocs#

CROCS: each meter as a Representative Load Set, compared by weighted shape distance.

Each meter’s days are clustered into a few representative day shapes with their frequencies (its Representative Load Set, RLS). Two meters are then compared by the frequency-weighted distance from each representative of one to the nearest representative of the other (WSMD), symmetrised. The result is a precomputed distance matrix for Agglomerative with metric="precomputed".

Functions

distances(sets)

Symmetric distance matrix (wsmd(a, b) + wsmd(b, a)) / 2 over every pair of load sets.

rls(days[, ks, n_init, random_state])

One meter's Representative Load Set, its number of day types chosen by silhouette.

wsmd(a, b)

Weighted shape-matching distance from one load set to another.

Classes

CROCS([ks, normalized, n_init, random_state])

Pairwise CROCS distances between meters, as a scikit-learn estimator.

RLS(centroids, weights)

A Representative Load Set: day shapes and how often each occurs.

class milia.represent.crocs.RLS(centroids, weights)#

Bases: object

A Representative Load Set: day shapes and how often each occurs.

Examples

>>> RLS(np.ones((2, 24)), np.array([0.7, 0.3])).k
2
property k: int#

Number of representative days.

Returns:

The size of the set.

milia.represent.crocs.rls(days, ks=(2, 3, 4), n_init=10, random_state=0)#

One meter’s Representative Load Set, its number of day types chosen by silhouette.

A meter whose days are too uniform to split gets a single representative, its mean day.

Parameters:
  • days (ndarray) – The meter’s days, shape (days, steps).

  • ks (Sequence[int]) – Candidate numbers of representative days.

  • n_init (int) – k-means restarts.

  • random_state (int) – Seed.

Return type:

RLS

Returns:

The meter’s RLS.

Examples

>>> noise = np.random.default_rng(0).normal(scale=0.01, size=(10, 4))
>>> days = np.vstack([np.zeros((5, 4)), np.ones((5, 4))]) + noise
>>> found = rls(days, ks=(2, 3))
>>> found.k, sorted(found.weights.tolist())
(2, [0.5, 0.5])
milia.represent.crocs.wsmd(a, b)#

Weighted shape-matching distance from one load set to another.

Each representative day of a is matched to its nearest day in b, and the distances are averaged with a’s frequencies. Not symmetric; see distances().

Parameters:
  • a (RLS) – Load set the weights come from.

  • b (RLS) – Load set matched against.

Return type:

float

Returns:

The distance.

Examples

>>> a = RLS(np.array([[0.0, 0.0]]), np.array([1.0]))
>>> b = RLS(np.array([[3.0, 4.0], [0.0, 1.0]]), np.array([0.5, 0.5]))
>>> wsmd(a, b)
1.0
milia.represent.crocs.distances(sets)#

Symmetric distance matrix (wsmd(a, b) + wsmd(b, a)) / 2 over every pair of load sets.

Parameters:

sets (Sequence[RLS]) – One RLS per meter.

Return type:

ndarray

Returns:

Square matrix with a zero diagonal, ready for a clusterer with metric="precomputed".

Examples

>>> a = RLS(np.array([[0.0, 0.0]]), np.array([1.0]))
>>> b = RLS(np.array([[3.0, 4.0]]), np.array([1.0]))
>>> distances([a, b]).tolist()
[[0.0, 5.0], [5.0, 0.0]]
class milia.represent.crocs.CROCS(ks=(2, 3, 4), normalized=True, n_init=10, random_state=0)#

Bases: BaseEstimator

Pairwise CROCS distances between meters, as a scikit-learn estimator.

Input is a (meters, days, steps) tensor; output of fit_transform() is the (meters, meters) distance matrix.

Parameters:
  • ks (tuple[int, ...]) – Candidate numbers of representative days per meter.

  • normalized (bool) – Peak-normalise each meter’s slice first.

  • n_init (int) – k-means restarts per meter.

  • random_state (int) – Seed.

Variables:
  • sets – Each meter’s RLS.

  • distances – The pairwise distance matrix.

Examples

>>> from milia import make
>>> from milia.data import profiles, synthetic
>>> cube = profiles.tensor(synthetic.generate(meters=6, days=14, steps=24).pivot)
>>> D = CROCS(ks=(2,)).fit_transform(cube)
>>> clusterer = make("clusterer", "agglomerative", n_clusters=3, linkage="average", metric="precomputed")
>>> clusterer.fit(D).n_clusters_
3
fit(X, y=None)#

Build each meter’s load set and the pairwise distances.

Parameters:
  • X (ndarray) – Tensor of shape (meters, days, steps).

  • y (None) – Ignored; present for API consistency.

Return type:

CROCS

Returns:

The fitted estimator.

Raises:

ValueError – If X is not three-dimensional.

Examples

>>> from milia.data import profiles, synthetic
>>> cube = profiles.tensor(synthetic.generate(meters=6, days=14, steps=24).pivot)
>>> CROCS(ks=(2,)).fit(cube).distances_.shape
(6, 6)
fit_transform(X, y=None)#

Fit and return the pairwise distance matrix.

Parameters:
  • X (ndarray) – Tensor of shape (meters, days, steps).

  • y (None) – Ignored; present for API consistency.

Return type:

ndarray

Returns:

The (meters, meters) distance matrix.