Skip to content

KMeansClustering no longer works with custom objects. #15

@exhuma

Description

@exhuma

The following code gives an error:

from cluster import KMeansClustering
from os import urandom
from pprint import pprint
from random import randint


class ObjectWithMetadata(object):

    def __init__(self, value):
        self.value = value
        self.uid = urandom(10).encode('base64').strip()

    def __repr__(self):
        return 'ObjectWithMetadata({!r}, {!r}'.format(self.value, self.uid)


data = [ObjectWithMetadata(randint(0, 1000))
        for _ in range(200)]

cl = KMeansClustering(data, lambda x, y: float(abs(x.value-y.value)))
clustered = cl.getclusters(10)
pprint(clustered)
print(len(clustered))

The error:

Traceback (most recent call last):
  File "metadata.py", line 21, in <module>
    clustered = cl.getclusters(10)
  File "/home/exhuma/work/github/python-cluster/cluster/method/kmeans.py", line 109, in getclusters
    res = self.assign_item(item, cluster)
  File "/home/exhuma/work/github/python-cluster/cluster/method/kmeans.py", line 124, in assign_item
    if self.distance(item, centroid(cluster)) < self.distance(
  File "/home/exhuma/work/github/python-cluster/cluster/util.py", line 175, in centroid
    for i in range(len(data[0])):
TypeError: object of type 'ObjectWithMetadata' has no len()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions