Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

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

Open
sc28 opened this issue May 13, 2018 · 3 comments
Open

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

sc28 opened this issue May 13, 2018 · 3 comments

Comments

@sc28
Copy link

sc28 commented May 13, 2018

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
Copy link
Author

sc28 commented May 13, 2018

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)

@vedavikas06
Copy link

##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

@TyrandeWhisperwind
Copy link

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants