K近邻[数学模型]——机器学习

import numpy as np
import matplotlib.pyplot as plt

#训练样本(前4个为A类,后3个为B类)
x_train = np.array([[4, 5], [6, 7], [4.8, 7], [5.5, 8], [7, 8], [10, 11], [9, 14]])
y_train = ['A', 'A', 'A', 'A', 'B', 'B', 'B']
#测试样本(6个)
x_test = np.array([[3.5, 7], [9, 13], [8.7, 10], [5, 6], [7.5, 8], [9.5, 12]])

plt.xlabel("X")
plt.ylabel("Y")
plt.title("KNN")
plt.plot(x_train[0:4,0],x_train[0:4,1],color='pink',marker='o',label='One Class A',linestyle='None')
plt.plot(x_train[4:7,0],x_train[4:7,1],color='c',marker='s',label='Two Class B',linestyle='None')
plt.plot(x_test[:,0],x_test[:,1],color='g',marker='^',label='?',linestyle='None')
for i in range(len(x_test)):
    plt.text(x_test[i,0]-0.3,x_test[i,1]+0.3, str(i) + '->?')
plt.legend(loc='upper left')
plt.grid(True)
plt.show()

#KNN分类
class KNN():
    #导入训练样本
    def fit(self, x_train, y_train):
        self.x_train = x_train
        self.y_train = y_train
    #使用反函数为近邻分配权重
    def inverse_weight(self,dist,num=1.0,const=0.1):
        return num/(dist+const)

    # 预测1个样本的类别
    def predict_once(self, x_test, k, T=0):  # T:普通/加权
        N = self.x_train.shape[0] #shape[0] :读取行数;shape[1]:读取列数
        x_test_ext = np.tile(x_test, [N, 1])  #就是把x_test先沿x轴复制1倍,即没有复制,仍然是 x_test。 再把结果沿y方向复制N倍
        euclidean_distance = np.sqrt(np.sum(np.power(np.subtract(x_test_ext, self.x_train), 2), 1)) #subtract:逐个元素对应相减
        inx = np.argsort(euclidean_distance) #返回列表中数值从小到大的索引
        if T == 0:  # 根据K个近邻中对应样本最多的类别进行分类
            # 累计每个类别对应的样本数
            class_predict = np.zeros(2)
            for i in range(k):
                idx = inx[i]
                if self.y_train[idx] == 'A':
                    class_predict[0] += 1
                else:
                    class_predict[1] += 1
            class_label = 'B' if np.argmax(class_predict) else 'A' #argmax判断最大值的索引,并返回索引下的A,B值
            return class_label

        else:  # 根据K个近邻中对应样本的距离权重进行分类
            distance_weight = np.zeros(2)  # 累加权重
            for i in range(k):
                idx = inx[i]
                if self.y_train[idx] == 'A':
                    distance_weight[0] += self.inverse_weight(euclidean_distance[idx])
                else:
                    distance_weight[1] += self.inverse_weight(euclidean_distance[idx])
            class_label = 'B' if np.argmax(distance_weight) else 'A'
            return class_label

    def predict(self, x_test, k, T=0):
        class_predict = []
        for i in range(len(x_test)):
            class_predict.append(self.predict_once(x_test[i, :], k, T))
        return class_predict

#KNN分类预测
knn = KNN()
knn.fit(x_train, y_train)
y_predict = knn.predict(x_test, 3, 0) #T=0/1
print(y_predict)
print("================")
#显示结果

plt.xlabel('X')
plt.ylabel('Y')
plt.title('KNN')
plt.plot(x_train[0:4,0], x_train[0:4,1], color='pink', marker='o', label='One Class (A)', linestyle='None') #显示”A”类
plt.plot(x_train[4:8,0], x_train[4:8,1], color='c', marker='s', label='Two Class (B)', linestyle='None') #显示”B”类
for i in range(len(x_test)): #显示预测结果
    if y_predict[i] == 'A':
            plt.plot(x_test[i,0], x_test[i,1], color='pink', marker='o')
            plt.text(x_test[i,0]-0.3, x_test[i,1]+0.3, str(i) + '->A')
    else:
            plt.plot(x_test[i,0], x_test[i,1], color='c', marker='s')
            plt.text(x_test[i,0]-0.3, x_test[i,1]+0.3, str(i) + '->B')
plt.legend(loc='upper left')
plt.grid(True)
plt.show()

 

 预测分类结果:

['A', 'B', 'B', 'A', 'A', 'B']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值