Skip to content

cluster_centroids

cluster_centroids_euclidean(positions, masses, cluster_indices)

Computes euclidean centroids for a set of clusters.

Parameters:

Name Type Description Default
positions (n, d) numpy float array

d-dimensional positions.

required
masses (n, ) numpy float array

Masses/weights for each of the n-positions.

required
cluster_indices (n, ) numpy int array

Cluster indices.

required

Returns:

Name Type Description
centroids (num_clusters, d) numpy float array

Centroids in euclidean space.

Source code in src\fast_cody\cluster_centroids.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def cluster_centroids_euclidean(positions, masses, cluster_indices):
    """ Computes euclidean centroids for a set of clusters.
    Parameters
    ----------
    positions : (n, d) numpy float array
        d-dimensional positions.
    masses : (n, ) numpy float array
        Masses/weights for each of the n-positions.
    cluster_indices : (n, ) numpy int array
        Cluster indices.

    Returns
    ------
    centroids : (num_clusters, d) numpy float array
        Centroids in euclidean space.
    """
    unique_clusters = np.unique(cluster_indices)
    centroids = np.zeros( (unique_clusters.shape[0] ,positions.shape[1] ))

    for cluster in unique_clusters:
        cluster_mask = (cluster_indices == cluster)
        cluster_positions = positions[cluster_mask]
        cluster_masses = masses[cluster_mask]

        weighted_sum = np.sum(cluster_positions * cluster_masses[:, np.newaxis], axis=0)
        total_mass = np.sum(cluster_masses)

        cluster_centroid = weighted_sum / total_mass
        centroids[cluster] =  cluster_centroid

    return centroids

cluster_centroids_spectral(B, cluster_indices)

Computes for clusters in spectral space.

Parameters:

Name Type Description Default
B (n, d) numpy float array

d-dimensional spectral feature vectors.

required
cluster_indices (n, ) numpy int array

Cluster indices.

required

Returns:

Name Type Description
centroids (num_clusters, d) numpy float array

Centroids in spectral space.

Source code in src\fast_cody\cluster_centroids.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def cluster_centroids_spectral( B, cluster_indices):
    """Computes for clusters in spectral space.

    Parameters
    ----------
    B : (n, d) numpy float array
        d-dimensional spectral feature vectors.
    cluster_indices : (n, ) numpy int array
        Cluster indices.

    Returns
    -------
    centroids : (num_clusters, d) numpy float array
        Centroids in spectral space.
    """
    # Determine the number of clusters
    num_clusters = np.max(cluster_indices) + 1
    # Initialize an array to store the centroids
    centroids = np.zeros((num_clusters, B.shape[1]))
    # Calculate centroids for each cluster
    for cluster_idx in range(num_clusters):
        cluster_mask = (cluster_indices == cluster_idx)
        cluster_points = B[cluster_mask, :]
        centroid = np.mean(cluster_points, axis=0)
        centroids[cluster_idx] = centroid
    return centroids