milia.plot.lets.clusters#

Clusters figures drawn with lets-plot (see milia.plot.figures.clusters).

milia.plot.lets.clusters.centroids(centroids, title=None, xlabel='Step of day', ylabel='Load')#

Every centroid on one axes.

Parameters:
Returns:

The figure – a lets-plot PlotSpec or a matplotlib Figure, 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.lets.clusters.dendrogram(X, method='ward', cut=None, title=None)#

Hierarchical-clustering dendrogram, with an optional cut line.

Parameters:
Returns:

The figure – a lets-plot PlotSpec or a matplotlib Figure, 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'
milia.plot.lets.clusters.grid(scores, title=None)#

Every metric of a sweep table, one panel each.

Parameters:
Returns:

The figure – a lets-plot PlotSpec or a matplotlib Figure, 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.lets.clusters.members(profiles, labels, cluster, title=None, xlabel='Step of day', ylabel='Load')#

All members of one cluster.

Parameters:
  • draw – Drawing backend; bound for you in milia.plot.lets and milia.plot.mpl.

  • profiles (ArrayLike) – Profiles, shape (entities, steps).

  • labels (ArrayLike) – Cluster label of each entity.

  • cluster (int) – The cluster drawn.

  • title (str | None) – Figure title; cluster <n> by default.

  • xlabel (str) – x-axis label.

  • ylabel (str) – y-axis label.

Returns:

The figure – a lets-plot PlotSpec or a matplotlib Figure, 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.lets.clusters.profiles(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:
Returns:

The figure – a lets-plot PlotSpec or a matplotlib Figure, 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.lets.clusters.scatter(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: markers gives each cluster its own marker shape, numbers writes its number (from 1, the label plus one) at its median, outline draws its convex hull in a pale tint, and highlight draws 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 – Drawing backend; bound for you in milia.plot.lets and milia.plot.mpl.

  • embedding (ArrayLike) – Coordinates, shape (n, 2) (further columns are ignored).

  • labels (ArrayLike) – Cluster label of each row.

  • title (str | None) – Figure title.

  • 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 PlotSpec or a matplotlib Figure, 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.lets.clusters.validity(scores, metrics=None, normalize=False, title=None)#

Validity metrics against k.

One panel per metric, or with normalize all metrics on one axis, each min-max scaled and oriented so higher is better.

Parameters:
Returns:

The figure – a lets-plot PlotSpec or a matplotlib Figure, 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