milia.plot.figures.clusters#
Cluster figures: embeddings, profiles, centroids, validity curves and dendrograms.
Functions
|
Every centroid on one axes. |
|
Hierarchical-clustering dendrogram, with an optional cut line. |
|
Every metric of a sweep table, one panel each. |
|
Profiles in long form: entity, cluster, step and value. |
|
All members of one cluster. |
|
Mean profile of each cluster, one panel per cluster, with its members drawn thin and grey behind it. |
|
Two-dimensional embedding coloured by cluster. |
|
Validity metrics against k. |
- milia.plot.figures.clusters.scatter(draw, embedding, labels, title=None, xlabel='dimension 1', ylabel='dimension 2', markers=False, numbers=False, outline=False, highlight=None)#
Two-dimensional embedding coloured by cluster.
Project first with
milia.represent.reduce.project(). With many clusters, colour alone cannot identify them:markersgives each cluster its own marker shape,numberswrites its number (from 1, the label plus one) at its median,outlinedraws its convex hull in a pale tint, andhighlightdraws one cluster, such as a minority that stands apart, in the highlight colour of the style. Neighbouring clusters then take the most different colours of the palette.- Parameters:
draw (
ModuleType) – Drawing backend; bound for you inmilia.plot.letsandmilia.plot.mpl.embedding (
ArrayLike) – Coordinates, shape(n, 2)(further columns are ignored).labels (
ArrayLike) – Cluster label of each row.xlabel (
str) – x-axis label.ylabel (
str) – y-axis label.markers (
bool) – Give each cluster its own marker shape.numbers (
bool) – Write the number of each cluster at its median.outline (
bool) – Draw the convex hull of each cluster.highlight (
int|None) – Label of the cluster drawn in the highlight colour.
- Returns:
The figure – a lets-plot
PlotSpecor a matplotlibFigure, depending on the backend.
Examples
>>> import numpy as np, pandas as pd >>> from milia.plot.mpl import clusters >>> figure = clusters.scatter(np.random.default_rng(0).normal(size=(10, 2)), [0, 1] * 5) >>> type(figure).__name__ 'Figure' >>> figure = clusters.scatter( ... np.random.default_rng(0).normal(size=(12, 2)), [0, 1, 2] * 4, markers=True, numbers=True, outline=True ... ) >>> type(figure).__name__ 'Figure'
- milia.plot.figures.clusters.long(profiles, labels)#
Profiles in long form: entity, cluster, step and value.
- Parameters:
- Return type:
- Returns:
One row per entity and step.
Examples
>>> import numpy as np >>> long(np.ones((2, 3)), [0, 1]).shape (6, 4)
- milia.plot.figures.clusters.profiles(draw, profiles, labels, title=None, xlabel='Step of day', ylabel='Normalised load')#
Mean profile of each cluster, one panel per cluster, with its members drawn thin and grey behind it.
The figure to check that a clustering of shapes means something.
- Parameters:
draw (
ModuleType) – Drawing backend; bound for you inmilia.plot.letsandmilia.plot.mpl.profiles (
ArrayLike) – Profiles, shape(entities, steps).labels (
ArrayLike) – Cluster label of each entity.xlabel (
str) – x-axis label.ylabel (
str) – y-axis label.
- Returns:
The figure – a lets-plot
PlotSpecor a matplotlibFigure, depending on the backend.
Examples
>>> import numpy as np, pandas as pd >>> from milia.plot.mpl import clusters >>> figure = clusters.profiles(np.random.default_rng(0).random((6, 24)), [0, 1, 2] * 2) >>> len(figure.axes) 3
- milia.plot.figures.clusters.centroids(draw, centroids, title=None, xlabel='Step of day', ylabel='Load')#
Every centroid on one axes.
- Parameters:
draw (
ModuleType) – Drawing backend; bound for you inmilia.plot.letsandmilia.plot.mpl.centroids (
ArrayLike) – Centroid profiles, shape(clusters, steps).xlabel (
str) – x-axis label.ylabel (
str) – y-axis label.
- Returns:
The figure – a lets-plot
PlotSpecor a matplotlibFigure, depending on the backend.
Examples
>>> import numpy as np, pandas as pd >>> from milia.plot.mpl import clusters >>> type(clusters.centroids(np.random.default_rng(0).random((3, 24)))).__name__ 'Figure'
- milia.plot.figures.clusters.members(draw, profiles, labels, cluster, title=None, xlabel='Step of day', ylabel='Load')#
All members of one cluster.
- Parameters:
draw (
ModuleType) – Drawing backend; bound for you inmilia.plot.letsandmilia.plot.mpl.profiles (
ArrayLike) – Profiles, shape(entities, steps).labels (
ArrayLike) – Cluster label of each entity.cluster (
int) – The cluster drawn.xlabel (
str) – x-axis label.ylabel (
str) – y-axis label.
- Returns:
The figure – a lets-plot
PlotSpecor a matplotlibFigure, depending on the backend.
Examples
>>> import numpy as np, pandas as pd >>> from milia.plot.mpl import clusters >>> type(clusters.members(np.random.default_rng(0).random((6, 24)), [0, 1] * 3, 0)).__name__ 'Figure'
- milia.plot.figures.clusters.validity(draw, scores, metrics=None, normalize=False, title=None)#
Validity metrics against k.
One panel per metric, or with
normalizeall metrics on one axis, each min-max scaled and oriented so higher is better.- Parameters:
draw (
ModuleType) – Drawing backend; bound for you inmilia.plot.letsandmilia.plot.mpl.scores (
DataFrame) – Sweep table frommilia.cluster.validation.sweep().metrics (
Sequence[str] |None) – Metrics drawn; every metric present by default.normalize (
bool) – Share one normalised axis.
- Returns:
The figure – a lets-plot
PlotSpecor a matplotlibFigure, depending on the backend.
Examples
>>> import numpy as np, pandas as pd >>> from milia.plot.mpl import clusters >>> scores = pd.DataFrame({"k": [2, 3, 4], "silhouette": [0.5, 0.8, 0.6], "davies": [0.7, 0.4, 0.6]}) >>> len(clusters.validity(scores).axes) 2
- milia.plot.figures.clusters.grid(draw, scores, title=None)#
Every metric of a sweep table, one panel each.
- Parameters:
draw (
ModuleType) – Drawing backend; bound for you inmilia.plot.letsandmilia.plot.mpl.scores (
DataFrame) – Sweep table.
- Returns:
The figure – a lets-plot
PlotSpecor a matplotlibFigure, depending on the backend.
Examples
>>> import numpy as np, pandas as pd >>> from milia.plot.mpl import clusters >>> len(clusters.grid(pd.DataFrame({"k": [2, 3], "silhouette": [0.5, 0.8]})).axes) 1
- milia.plot.figures.clusters.dendrogram(draw, X, method='ward', cut=None, title=None)#
Hierarchical-clustering dendrogram, with an optional cut line.
- Parameters:
draw (
ModuleType) – Drawing backend; bound for you inmilia.plot.letsandmilia.plot.mpl.X (
ArrayLike) – Data.method (
str) – Linkage method of scipy.cut (
int|None) – Draw the height that cuts the tree into this many clusters.
- Returns:
The figure – a lets-plot
PlotSpecor a matplotlibFigure, depending on the backend.
Examples
>>> import numpy as np, pandas as pd >>> from milia.plot.mpl import clusters >>> type(clusters.dendrogram(np.random.default_rng(0).normal(size=(12, 2)), cut=3)).__name__ 'Figure'