# K-Means for Tensors

Is there some clean way to do K-Means clustering on Tensor data without converting it to numpy array.

I have a list of tensors and their corresponding labes and this is what I am doing.

def evaluateKMeansRaw(data, true_labels, n_clusters):

``````kmeans = KMeans(n_clusters=n_clusters,n_init=20)
kmeans.fit(data)

acc = cluster_acc(true_labels, kmeans.labels_)
nmi = metrics.normalized_mutual_info_score(true_labels, kmeans.labels_)
return acc, nmi
``````

But this doesn’t work on the output of a Conv Layer

You cannot use scikit-learn on tensors as scikit-learn (and all of its methods) only work on numpy arrays.

However if you are not afraid to use custom implementations you could use something like this

1 Like

Do we not have these algorithms as part of the framework as of now ?

check out this github repo. can be installed in a breeze using pip:

`pip install kmeans-pytorch`

find documentation here

1 Like

I implemented NN, KNN and KMeans on a project I am working on only using PyTorch. You can find the implementation here with an example: Nearest Neighbor, K Nearest Neighbor and K Means (NN, KNN, KMeans) only using PyTorch · GitHub

``````>>> import torch as th
>>> from clustering import KNN
>>> data = th.Tensor([[1, 1], [0.88, 0.90], [-1, -1], [-1, -0.88]])
>>> labels = th.LongTensor([3, 3, 5, 5])
>>> test = th.Tensor([[-0.5, -0.5], [0.88, 0.88]])
>>> knn = KNN(data, labels)
>>> knn(test)
tensor([5, 3])
``````