1

I'm trying to run the kmedoids clustering implementation available on this github page.

The provided minimal working example is pretty straightforward, yet I can't manage to execute the first line using the kMedoids() function without raising an error:

from sklearn.metrics.pairwise import pairwise_distances
import numpy as np    
import kmedoids

# 3 points in dataset
data = np.array([[1,1], 
                [2,2], 
                [10,10]])

# distance matrix
D = pairwise_distances(data, metric='euclidean')

# split into 2 clusters
M, C = kmedoids.kMedoids(D, 2)  # <-- THIS RAISES AN ERROR

print('medoids:')
for point_idx in M:
    print( data[point_idx] )

print('')
print('clustering result:')
for label in C:
    for point_idx in C[label]:
        print('label {0}: {1}'.format(label, data[point_idx]))

Error is:

Traceback (most recent call last):
File "/usr/lib/python3.5/code.py", line 91, in runcode
exec(code, self.locals)
File "", line 1, in 
File "", line 9, in kMedoids
File "mtrand.pyx", line 4832, in mtrand.RandomState.shuffle
File "mtrand.pyx", line 4835, in mtrand.RandomState.shuffle
TypeError: 'range' object does not support item assignment

I set up the example in Eclipse PyDev as follows for Python 3.5:

  • Installed all modules using pip3 install (numpy, scipy and scikit-learn)
  • Added the kmedoids.py file in the same directory as example.py

Has anyone tried using this function recently? Could my version of Python (3.5) be causing this error?

sc28
  • 1,163
  • 4
  • 26
  • 48

1 Answers1

1

Found the issues, indeed certainly related to the kMedoids() code which wasn't intended initially for Python 3.

To make it work for Python 3.5, edit the following lines related to the range() function as follows (cf. this related answer):

index_shuf = range(len(rs)) -->  index_shuf = list(range(len(rs)))

and

for t in xrange(tmax): --> for t in range(tmax):
sc28
  • 1,163
  • 4
  • 26
  • 48