Find clusters in your data, choose their number, and inspect whether the result agrees with the structure visible in a topographic map.
A two-dimensional scatter plot can hide groups or suggest gaps that
do not exist in the original data.
ProjectionBasedClustering combines a projection with a
topographic map that uses distances in the original data space. Valleys
represent groups of similar observations; ridges help you see where
groups separate. This gives you a way to assess cluster structure before
choosing a cluster count and to inspect the resulting assignments
afterwards.
The package supports two ways of working:
Both workflows use the same data and map representation. You can try different projection methods and inspect how well each one reveals the structures you want to analyse.
Install from CRAN, including optional R dependencies used by the projection methods:
install.packages("ProjectionBasedClustering", dependencies = TRUE)Alternatively, install the development version from GitHub with
remotes:
remotes::install_github("Mthrun/ProjectionBasedClustering")For the GitHub installation, install any missing optional dependencies for your selected projection method separately. Python setup is described in Use a Python projection from R.
Start by exploring the data before assigning cluster labels. The
example uses the included Hepta dataset, which contains 212
observations with three variables. Replace Data with your
own numeric matrix, with observations in rows and variables in columns.
Choose variables and scaling so that distances between observations have
a useful meaning for your application.
library(ProjectionBasedClustering)
library(GeneralizedUmatrix)
data("Hepta", package = "ProjectionBasedClustering")
Data = Hepta$Data
set.seed(1)
ProjectedPoints = NeRV(Data, randominit = FALSE)
PlotProjectedPoints(ProjectedPoints)The scatter plot shows the projected positions. To assess their
separation using information from the original data, add a topographic
map with GeneralizedUmatrix(). Compare its valleys and
ridges with the groups you see in the scatter plot. This is the visual
assessment used in IPBC (Thrun et al.,
2021).
Map = GeneralizedUmatrix(Data, ProjectedPoints, Parallel = FALSE)
Umatrix = Map$Umatrix
Bestmatches = Map$Bestmatches
Lines = Map$Lines
Columns = Map$Columns
TopviewTopographicMap(Umatrix, Bestmatches)Look for groups of points in valleys separated by ridges. These
groups help you choose a cluster count for the next step. If the map
shows no clear separation, reconsider whether a division into clusters
is useful. You can also try another projection: replace the
NeRV() call and recompute the map. Keep the projected
points in the same row order as Data.
Continue with the data and map from the previous example. Once you
have selected a cluster count, ProjectionBasedClustering()
assigns observations to groups using the projection neighbourhoods and
distances in the original data. This implements PBC for distance- and
density-based cluster structures (Thrun and Ultsch,
2020).
The example uses the known seven groups of Hepta. For
your own data, choose k from your assessment of the map.
StructureType = TRUE selects the mode for compact groups;
use FALSE for connected groups.
k = 7
Cls = ProjectionBasedClustering(k = k, DataOrDistances = Data,
BestMatches = Bestmatches,
LC = c(Lines, Columns),
StructureType = TRUE)
TopviewTopographicMap(Umatrix, Bestmatches, Cls)
table(Cls)For this example, the cluster sizes are:
Cls
1 2 3 4 5 6 7
32 30 30 30 30 30 30
Cls contains one cluster label per observation, in the
row order of Data. You can use these labels to compare
variables between groups or to select observations for further analysis.
The coloured map lets you inspect whether the assignments follow the
valleys and ridges. It also helps you identify places where you should
reconsider the projection, cluster count, or selected structure type.
The dataset’s reference labels are not used to fit the projection or
compute the clusters.
Use IPBC() when you want to explore projection methods
and define groups with the mouse. The interface combines the map and
clustering controls, so you can use domain knowledge while inspecting
the data. The following call starts the interactive workflow introduced
by Thrun et
al. (2020).
library(ProjectionBasedClustering)
data("Hepta", package = "ProjectionBasedClustering")
Data = Hepta$Data
Result = IPBC(Data)
Cls = Result$Cls
table(Cls)In the interface:
Cls.interactiveProjectionBasedClustering() is the full name
of the same interface. You can also pass existing labels with
IPBC(Data, Cls) to inspect or refine a previous clustering.
The result includes the projection and map as well as the cluster
labels; see ?interactiveProjectionBasedClustering for all
returned components.
You can use a projection implemented in Python and continue with the
same R workflow shown above. The following example connects
DensMAP() to the Python package umap-learn
through the optional R package reticulate.
Set up the environment once:
install.packages("reticulate")
reticulate::virtualenv_create("r-densmap")
reticulate::py_install("umap-learn>=0.5", envname = "r-densmap",
method = "virtualenv")In each new R session, select the environment before the first Python call:
reticulate::use_virtualenv("r-densmap", required = TRUE)
library(ProjectionBasedClustering)
data("Hepta", package = "ProjectionBasedClustering")
Data = Hepta$Data
Projection = DensMAP(Data, k = 15, Epochs = 300,
random_state = 42, n_jobs = 1L)
ProjectedPoints = Projection$ProjectedPoints
PlotProjectedPoints(ProjectedPoints)An existing environment with umap-learn >= 0.5 can
also be selected. DensMAP() does not install Python
packages. If the Python environment or a required package is
unavailable, it reports the problem and returns
ProjectedPoints = NULL; complete the setup before
continuing.
To inspect and cluster this projection, repeat the
GeneralizedUmatrix() and
ProjectionBasedClustering() steps above with the new
ProjectedPoints. See ?DensMAP for further
arguments and reproducibility notes.
[Thrun et al., 2021] Thrun, M. C., Pape, F., & Ultsch, A.: Conventional Displays of Structures in Data Compared With Interactive Projection-Based Clustering (IPBC). International Journal of Data Science and Analytics, Vol. 12(3), pp. 249-271, Springer, 2021. DOI: 10.1007/s41060-021-00264-2.
[Thrun and Ultsch, 2020] Thrun, M. C., & Ultsch, A.: Using Projection-Based Clustering to Find Distance- and Density-Based Clusters in High-Dimensional Data. Journal of Classification, Vol. 38(2), pp. 280-312, Springer. Published online in 2020; issue publication in 2021. DOI: 10.1007/s00357-020-09373-2.
[Thrun et al., 2020] Thrun, M. C., Pape, F., & Ultsch, A.: Interactive Machine Learning Tool for Clustering in Visual Analytics. 7th IEEE International Conference on Data Science and Advanced Analytics (DSAA 2020), pp. 479-487, IEEE, Sydney, Australia, 2020. DOI: 10.1109/DSAA49011.2020.00062.