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
|
Symmetric distance matrix |
|
One meter's Representative Load Set, its number of day types chosen by silhouette. |
|
Weighted shape-matching distance from one load set to another. |
Classes
|
Pairwise CROCS distances between meters, as a scikit-learn estimator. |
|
A Representative Load Set: day shapes and how often each occurs. |
- class milia.represent.crocs.RLS(centroids, weights)#
Bases:
objectA Representative Load Set: day shapes and how often each occurs.
Examples
>>> RLS(np.ones((2, 24)), np.array([0.7, 0.3])).k 2
- 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:
- Return type:
- 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
ais matched to its nearest day inb, and the distances are averaged witha’s frequencies. Not symmetric; seedistances().- Parameters:
- Return type:
- 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)) / 2over every pair of load sets.- Parameters:
- Return type:
- 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:
BaseEstimatorPairwise CROCS distances between meters, as a scikit-learn estimator.
Input is a
(meters, days, steps)tensor; output offit_transform()is the(meters, meters)distance matrix.- Parameters:
- 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:
- Return type:
- Returns:
The fitted estimator.
- Raises:
ValueError – If
Xis 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)