KNN基本实现

import numpy as np
import collections
import operator

def dataSet():
group=np.array[[1,201],[2,302],[20,230],[30.400],[2,600]]
labels=[‘爱情片’,‘爱情片’,‘动作片’,‘动作片’]
return group,labels
def classify0(index,dataset,labels,k):
dist=np.sum((index-dataset)**2,axis=1)**0.5
labels_k=[lables[index] for index in dist.argsort()[0:k]]
label=collections.Counter(labels_k).most_common(1)[0][0]
return label

if_name _ == ‘main’:
group,labels=dataSet()
test=[20,203]
test_calss=classify0(test,group,lables,3)
print(test_calss)

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读