繁体   English   中英

Python不可用类型:'numpy.ndarray'

[英]Python unhashable type: 'numpy.ndarray'

我致力于为K Nearest Neighbors制作函数。 我已经分别测试了每个功能,它们都运行良好。 但是每当我把它们放在一起并运行KNN_method ,它就会显示出unhashable type: 'numpy.ndarray' 这是我的代码:

def distance(p,point):
    import numpy as np
    value = np.sqrt(sum(np.power((p-point),2)))
    return(value)

def find_neighbors(p,list_of_points, k = 3):
    import numpy as np
    distances = np.zeros(list_of_points.shape[0])
    for i in range(list_of_points.shape[0]):
        distances[i]= distance(p,list_of_points[i])
    ind = np.argsort(distances)
    return(ind[0:k])

def majority_votes(votes):
    import random
    vote_result = {}
    for key in votes:
        if key in vote_result:
            vote_result[key] += 1
        else:
            vote_result[key] = 1
    final_list = []
    for (number, vote) in vote_result.items():
        if vote == max(vote_result.values()):
            final_list.append(number)
    Winner = random.choice(final_list)
    return(Winner)


def KNN_method(p , list_of_points , outcomes , k = 3):
    ind = find_neighbors(p , list_of_points , k) 
    Final = majority_votes(outcomes[ind])
    return(Final)

首先转换为元组。

hash(tuple(np.array([1,2,3,4])))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM