def getResponse(neighbors):classVotes = {}for x in range(len(neighbors)):response = neighbors[x][-1]if response in classVotes:classVotes[response] += 1else:classVotes[response] = 1sortedVotes = sorted(classVotes.iteritems(), key=operator.itemgetter(1), reverse=True)return sortedVotes[0][0]
1 回答

蠱毒傳說
TA貢獻1895條經(jīng)驗 獲得超3個贊
def getResponse(neighbors):
#給結(jié)果搞個字典
classVotes={}
#把每個neighor都循環(huán)一遍
for x in range(len(neighbors)):
response = neighbors[x][-1]
print response
if response in classVotes:
classVotes[response]+=1
print classVotes
else:
classVotes[response] =1
sortedVotes = sorted(classVotes.iteritems(),key = operator.itemgetter(1),reverse = True)
#reverse是按降序方式排序
return sortedVotes[0][0]
添加回答
舉報
0/150
提交
取消