执行kmedoids python模块
我正在尝试运行此服务器上可用的执行kmedoids python模块,python,python-3.x,k-means,Python,Python 3.x,K Means,我正在尝试运行此服务器上可用的kmedoids集群实现 提供的代码非常简单,但我无法使用kMedoids()函数执行第一行,而不会引发错误: from sklearn.metrics.pairwise import pairwise_distances import numpy as np import kmedoids # 3 points in dataset data = np.array([[1,1], [2,2],
kmedoids
集群实现
提供的代码非常简单,但我无法使用kMedoids()
函数执行第一行,而不会引发错误:
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]))
我在Eclipse PyDev中为Python 3.5设置了以下示例:
- 使用
(numpy、scipy和scikit学习)pip3安装安装所有模块
- 将
文件添加到与kmedoids.py
example.py
最近有人试过使用这个功能吗?我的Python版本(3.5)会导致此错误吗?发现了问题,确实与最初不适用于Python 3的
kMedoids()
有关
要使其适用于Python 3.5,请编辑与range()
函数相关的以下行,如下所示(参见此相关部分):
及
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
index_shuf = range(len(rs)) --> index_shuf = list(range(len(rs)))
for t in xrange(tmax): --> for t in range(tmax):