How to get variance inside each cluster?

Hello! I have this code and I want to get the variance inside each cluster. Thanks for helping.

import torchvision.datasets as dsets
train_dataset = dsets.MNIST(root=’./data’, train=True, transform=transforms.ToTensor(), download=True)
from sklearn.cluster import KMeans
num_class=10
data = train_dataset.data.numpy()
data=data.reshape(-1,28*28)
kmeans = KMeans(n_clusters=num_class, random_state=0).fit(data)
center=kmeans.cluster_centers_
centers=torch.from_numpy(center)

This doesn’t seem to be PyTorch-specific, as you are using sklearn.
You could probably use the labels_ to get all samples for the clusters and then calculate the variance between these samples.

1 Like