【问题标题】:TypeError: unhashable type: 'numpy.ndarray' in PythonTypeError:不可散列的类型:Python中的'numpy.ndarray'
【发布时间】:2020-05-22 15:18:09
【问题描述】:

我正在从包含平面上的点的数据集中读取数据。每个点都有 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 聚类。我已将中心点存储在列表中。然后我将检查距中心点特定半径内的点是什么。通过这种方式,我正在计算覆盖率。

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'

【问题讨论】:

    标签: python python-3.x hashmap set hashtable


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 2022-01-08
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多