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
data =
kmeans = KMeans(n_clusters=num_class, random_state=0).fit(data)

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