numpy实现简单knn

import numpy as np 
import matplotlib.pyplot as plt  
w=250
train=np.random.randint(-300,300,(w,2))
train=train.astype(float)
train_lable=np.zeros(w)
for i in range(2):
    train[:,i]=(train[:,i]-train[:,i].mean())/train[:,i].std()
for i in range(250):
    if train[i,0]>train[i,1]:
        plt.scatter(train[i,0], train[i,1],c='b')
        train_lable[i]=1
    else:
        plt.scatter(train[i,0], train[i,1],c='c')
        train_lable[i]=-1
plt.axis('equal')
plt.show()
def knn(index,k):
    dis=np.zeros(200)
    disl=[]
    for i in range(200):
        dis[i]=(train[index,0]-train[i,0])*(train[index,0]-train[i,0])+(train[index,1]-train[i,1])*(train[index,1]-train[i,1])
        disl.append(dis[i])
    target=[]
    for i in range(k):
        target.append(disl.index(min(disl)))
        disl[disl.index(min(disl))]=float('inf')
    l1=0
    l2=0
    for i in range(k):
        if train_lable[target[i]]==1:
            l1=l1+1
        else:
            l2=l2+1
    if l1>l2 :
        return 1
    else:
        return -1

sum1=0
for i in range(50):
    lable=knn(i+200,3)
    if lable==train_lable[i+200]:
        sum1=sum1+1
print(sum1)       

 

 

posted @ 2021-08-09 09:28  祥瑞哈哈哈  阅读(108)  评论(0编辑  收藏  举报