使用KNN对点进行二分类

实现一个KNN对坐标点进行二分类分算法,将四个点分为两类,标签是:“A”,“B”,其中用到了matplotlib对图像进行处理。

import numpy as np
import matplotlib.pyplot as plt #画图,把几个要分类的点画出来
import operator

def createDataSet():#分类数据
    group = np.array([   #knn是监督学习,要有标签
        [1.0,1.1],        #二维数组
        [1.0,1.0],
        [0,0],
        [0,0.1]
    ])
    labels = ['A','A','B','B']#标签A,B类
    return group,labels


def show_data(group,labels):
    labels = np.array(labels)
    index_a = np.where(labels == "A") #输出A的索引  where很重要
    index_b = np.where(labels == "B")  #输出B的索引
    for i in labels:#注意这个for循环,比较重要
        if i == "A":
            plt.scatter(group[index_a][:,:1],group[index_a][:,1:2],c='red')#画点 plt.scatter(x,y,c)可以画张量 plt.plot只能画标量
        elif i == "B":
            plt.scatter(group[index_b][:,:1],group[index_b][:,1:2],c="green")
    plt.show()


def classify(inX,dataset,labels,k):#x是待分类新数据,没有标签
    dataSetSize = dataset.shape[0]#看数据的形状,检验数据正确与否
    diffMat = np.tile(inX,(dataSetSize,1))-dataset #将X按1轴广播,减去原有的数据得到差值
    sqDiffMat = diffMat**2
    sqDistance = sqDiffMat.sum(axis=1)#按1轴求和
    distance = sqDistance ** 0.5#开方

    sortedDistanceIndex = distance.argsort()#排序(排索引)由小到大
    print(sortedDistanceIndex.shape)

    #存放最终的投票结果
    classCount = {}#字典

    for i in range(k):
        voteIlabel = labels[sortedDistanceIndex[i]]#取出邻居

        classCount[voteIlabel] = classCount.get(voteIlabel,0)+1
    print(classCount)
    sortedClassCount = sorted(classCount.items(),key=operator.itemgetter(1),reverse=True)#对字典按值排序,反向排序
    return sortedClassCount[0][0] #取键




if __name__ == '__main__':
    #导入数据
    dataSet,labels = createDataSet()
    #新数据
    inX = [0.5,0.5]
    className = classify(inX,dataSet,labels,3)
    print("该数据属于{}类".format(className))
    dataSet = np.vstack((dataSet,inX))#vstack:纵向堆叠,必须是元组
    labels.append(className)
    show_data(dataSet,labels)

运行结果:将五个点分为了两类
转载或引用请注明来源!

发布了18 篇原创文章 · 获赞 2 · 访问量 352

猜你喜欢

转载自blog.csdn.net/weixin_44928646/article/details/104517175