AAclust.comp_centers

static AAclust.comp_centers(X, labels)[source]

Computes the center of each cluster based on the given labels.

Each cluster center is the element-wise mean of all samples assigned to that cluster [Breimann24a]. Use this method independently of AAclust.fit() when cluster labels are already available.

Added in version 0.1.0.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – Feature matrix. Rows typically correspond to scales and columns to amino acids.

  • labels (array-like, shape (n_samples,)) – Cluster labels for each sample in X.

Returns:

  • centers (array-like, shape (n_clusters, n_features)) – The computed center for each cluster.

  • labels_centers (array-like, shape (n_clusters,)) – The labels associated with each computed center.

Examples

The cluster centers can be computed using the AAclust().comp_centers() method:

import aaanalysis as aa
import pandas as pd
# Create example dataset comprising 100 scales
df_scales = aa.load_scales().T.sample(100).T
X = df_scales.T
# Fit AAclust model and obtain clustering centers for 5 clusters
aac = aa.AAclust()
labels = aac.fit(X, n_clusters=5).labels_
centers, labels_centers = aac.comp_centers(X=X, labels=labels)
# Create DataFrame with cluster centers
columns = [f"Cluster {i}" for i in labels_centers]
df_centers = pd.DataFrame(centers.T, columns=columns, index=df_scales.index)
aa.display_df(df_centers)
  Cluster 2 Cluster 3 Cluster 1 Cluster 0 Cluster 4
AA          
A 0.439000 0.235000 0.425000 0.540000 0.233000
C 0.592000 0.111000 0.286000 0.488000 0.317000
D 0.161000 0.700000 0.755000 0.460000 0.455000
E 0.146000 0.753000 0.538000 0.646000 0.267000
F 0.818000 0.093000 0.336000 0.695000 0.219000
G 0.340000 0.245000 0.845000 0.297000 0.682000
H 0.386000 0.415000 0.513000 0.603000 0.343000
I 0.878000 0.071000 0.280000 0.663000 0.118000
K 0.157000 0.856000 0.638000 0.761000 0.280000
L 0.797000 0.073000 0.343000 0.709000 0.121000
M 0.668000 0.182000 0.236000 0.725000 0.193000
N 0.245000 0.530000 0.766000 0.521000 0.567000
P 0.311000 0.398000 0.772000 0.336000 0.764000
Q 0.236000 0.641000 0.584000 0.649000 0.261000
R 0.260000 0.732000 0.536000 0.746000 0.286000
S 0.296000 0.412000 0.767000 0.423000 0.456000
T 0.360000 0.375000 0.621000 0.409000 0.307000
V 0.741000 0.102000 0.300000 0.590000 0.119000
W 0.703000 0.248000 0.268000 0.759000 0.206000
Y 0.635000 0.345000 0.483000 0.683000 0.309000