Skip to content

8. Clustering in Euclidean distances

Jonas Schaub edited this page Nov 13, 2025 · 1 revision

As an alternative to the "standard" ART-2a clustering, this library also offers the Euclidean variant of ART-2a, i.e. performing clustering in the Euclidean space without the projection on the unit sphere. For this purpose, the Art2aEuclidKernel class needs to be used.

// Prepare your data as a 2D float array
// Each row represents a data vector
float[][] dataMatrix = {
        {0.1f, 0.2f, 0.3f},
        {0.2f, 0.1f, 0.4f},
        {0.9f, 0.8f, 0.7f},
        {0.8f, 0.9f, 0.6f},
        {0.15f, 0.25f, 0.35f},
        {0.85f, 0.75f, 0.65f},
        {0.05f, 0.15f, 0.25f},
        {0.95f, 0.85f, 0.75f},
        {0.12f, 0.22f, 0.32f},
        {0.88f, 0.78f, 0.68f}
};

// Configure clustering parameters
//Vigilance parameter in interval [0,1]
float vigilance = 0.5f;
//Maximum number of clusters in interval [2, number of data row vectors of dataMatrix]
int maximumNumberOfClusters = 10;
//default value
int maximumNumberOfEpochs = 10;
//default value
float convergenceThreshold = 0.99f;
//default value
float learningParameter = 0.01f;
//default value
float offsetForContrastEnhancement = 1.0f;
//default value
long randomSeed = 1L;
//do not preprocess data because it is already in range [0,1]
boolean isDataPreprocessing = false;

// Validate data matrix (same length in all rows, no empty rows, etc.)
if (Utils.isDataMatrixValid(dataMatrix)) {
    // Create ART-2a-Euclid kernel
    Art2aEuclidKernel art2aEuclidKernel = new Art2aEuclidKernel(
            dataMatrix,
            maximumNumberOfClusters,
            maximumNumberOfEpochs,
            convergenceThreshold,
            learningParameter,
            offsetForContrastEnhancement,
            randomSeed,
            isDataPreprocessing
    );

    // Perform clustering with set vigilance parameter and do the determination 
    //  of which cluster is closest to the new data point sequentially
    Art2aEuclidResult result = art2aEuclidKernel.getClusterResult(vigilance, false);

    // Access results
    int numberOfClusters = result.getNumberOfDetectedClusters();
    System.out.println("Number of epochs: " + result.getNumberOfEpochs());
    System.out.println("Is converged: " + result.isConverged());

    // Get cluster information
    for (int i = 0; i < numberOfClusters; i++) {
        System.out.println("Cluster " + i + ":");
        System.out.println("\tSize: " + result.getClusterSize(i));
        System.out.println("\tMembers: " + Arrays.toString(result.getDataVectorIndicesOfCluster(i)));
        System.out.println("\tRepresentative Index: " + result.getClusterRepresentativeIndex(i));
    }

    // Calculate distances between all cluster pairs
    for (int i = 0; i < numberOfClusters; i++) {
        for (int j = i + 1; j < numberOfClusters; j++) {
            double distance = result.getDistanceBetweenClusters(i, j);
            System.out.println("Distance between cluster " + i +
                    " and " + j + ": " + distance);
        }
    }
}


Output:

Number of epochs: 2
Is converged: true
Cluster 0:
	Size: 5
	Members: [0, 1, 4, 6, 8]
	Representative Index: 0
Cluster 1:
	Size: 5
	Members: [2, 3, 5, 7, 9]
	Representative Index: 9
Distance between cluster 0 and 1: 1.5223907232284546

Clone this wiki locally