kmedoids icon indicating copy to clipboard operation
kmedoids copied to clipboard

"TypeError: 'range' object does not support item assignment"

Open sc28 opened this issue 6 years ago • 3 comments

Hi, I tried to get the kMedoids function to work on the provided example but when running the following line:

# split into 2 clusters
M, C = kMedoids(D, 2)

I get the following error:

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

Any idea what is causing this? Should it work on Python 3.5?

sc28 avatar May 13 '18 12:05 sc28

I was able to fix this by replacing the following lines in kmedoids.py:

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

and

for t in xrange(tmax): --> for t in range(tmax):

This is due apparently to changes in the range() function's behavior from Python 2 to 3 (see comments here)

sc28 avatar May 13 '18 13:05 sc28

##1st change valid_medoid_inds = [i for i in range(n)] invalid_medoid_inds = [] rs,cs = np.where(D==0) # the rows, cols must be shuffled because we will keep the first duplicate below index_shuf = [i for i in range(len(rs))] np.random.shuffle(index_shuf) rs = rs[index_shuf] cs = cs[index_shuf] for r,c in zip(rs,cs): # if there are two points with a distance of 0... # keep the first one for cluster init if r < c and r not in invalid_medoid_inds: invalid_medoid_inds.append(c) valid_medoid_inds = [i for i in valid_medoid_inds+invalid_medoid_inds if i in valid_medoid_inds and i not in invalid_medoid_inds]

#2nd change for t in range(tmax):

For python3 users

vedavikas06 avatar Jun 27 '18 12:06 vedavikas06

what is the tmax varaible for? and why did u set it to 100? if i have a dataset of 800 points should i put tmax=800?

TyrandeWhisperwind avatar Apr 10 '19 11:04 TyrandeWhisperwind