我正在从包含平面中点的数据集中读取数据。每个点都有x和y坐标。
with open('SJC324.txt') as f:
data=[]
for line in f:
x,y=(line.strip('\n').split())
data.append([int(x),int(y)])
initialisation(data)
然后,我在这些点上进行了K-medoid聚类。我已将medoid点存储在列表中。然后,我将检查距这些类固醇特定半径内的哪些点。通过这种方式,我可以计算覆盖率。
def initialisation(data):
data=np.array(data)
D=pairwise_distances(data,metric='euclidean')
coverage=[]
for i in range(20):
covered_point=set()
M, C = kmedoids.kMedoids(D, len(data)//15)
medoid=data[M]
for temp in medoid:
for x in data:
if check_within_radius(temp,x):
covered_point.add(x)
coverage.append((len(covered_point)/len(data))*100)
print(coverage)
在这里,我正在检查哪些点位于这些类固醇的特定半径内。
def check_within_radius(temp,x):
#temp is medoid point
#x is any random point
radius=10
if (((temp[0]-x[0])**2) + ((temp[1]-x[1])**2))< radius*radius:
return True
else:
return False
现在我遇到以下错误。
<ipython-input-23-d04cdfb631a8> in initialisation(data)
16 for x in data:
17 if check_within_radius(temp,x):
---> 18 covered_point.add(x)
19 coverage.append((len(covered_point)/len(data))*100)
20 print(coverage)
TypeError: unhashable type: 'numpy.ndarray'
答案 0 :(得分:1)
您正在尝试在2D列表中查找唯一元素。您可以稍微修改一下代码。
from collections import Counter
temp = Counter([tuple(x) for x in covered_point])
#Counter will count the frequency of each element(1D list) in a 2D list
z = [list(k) for k, v in temp.items() if v >= 1]
'''
When one element(1D list) appears more than once in that 2D list you are
taking that only one time.
'''
coverage.append((len(z)/len(data))*100)