python实现KNN算法

具体代码如下

import numpy as np
import matplotlib.pyplot as plt


class myKNN():
    def __init__(self, k):
        self.k = k

    def compute(self, X_train, y_train, X_test):
        dist = []
        i = 0
        # 计算欧式距离
        for x in X_train:
            # dist[i] = np.sqrt(np.sum(np.square(x-X_test)))形式错误,要用append对空列表进行赋值
            dist.append(np.sqrt(np.sum(np.square(x-X_test))))
            i += 1

        # 对欧式距离进行排序, 去除前k个小的值, dist_k表示前k个小的值的下标
        dist_k = np.argsort(dist)[0:self.k]
        # 先取出k个最小距离的y值大小
        y_train_k = []
        for y in dist_k:
            y_train_k.append(y_train[y])
        y_train_k = np.array(y_train_k)
        y_train_k = y_train_k + 1
        return np.argmax(np.bincount(y_train_k))


def draw(X_train, y_train, X_new):
    # 正负实例点初始化
    X_po=np.zeros(X_train.shape[1])
    X_ne=np.zeros(X_train.shape[1])
    # 区分正、负实例点
    for i in range(y_train.shape[0]):
        if y_train[i]==1:
            X_po=np.vstack((X_po,X_train[i]))
        else:
            X_ne=np.vstack((X_ne,X_train[i]))
    # 实例点绘图
    plt.plot(X_po[1:,0],X_po[1:,1],"g*", label="1")
    plt.plot(X_ne[1:, 0], X_ne[1:, 1], "rx", label="-1")
    plt.plot(X_new[:, 0], X_new[:, 1], "bo", label="test_points")
    # 测试点坐标值标注
    for xy in zip(X_new[:, 0], X_new[:, 1]):
        plt.annotate("test{}".format(xy),xy)
    # 设置坐标轴
    plt.axis([0,10,0,10])
    plt.xlabel("x1")
    plt.ylabel("x2")
    # 显示图例
    plt.legend()
    # 显示图像
    plt.show()



def main():
    # 训练数据
    X_train = np.array([[5, 4],
                        [9, 6],
                        [4, 7],
                        [2, 3],
                        [8, 1],
                        [7, 2]])
    y_train = np.array([1, 1, 1, -1, -1, -1])
    # 测试数据
    X_new = np.array([[5, 3]])
    k = 3
    KNN = myKNN(k)
    if KNN.compute(X_train, y_train, X_new) == 0:
        print('当k={}, 测试数据被分类为{}'.format(k, -1))
    else:
        print('当k={}, 测试数据被分类为{}'.format(k, 1))
    draw(X_train, y_train, X_new)


if __name__ == '__main__':
    main()

输出如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值