欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

手动实现最邻近算法 KNN算法(分类 回归)

程序员文章站 2022-06-13 21:09:59
#数据# 特征raw_data_x= [[3.393533211,2.331273381], [2.110073483,1.781539638], [1.343808831,3.368360954], [3.582294042,4.679179110], [2.280362439,2.866990263], [7.423436942,4.696522875], ....
#数据
# 特征
raw_data_x= [[3.393533211,2.331273381],
             [2.110073483,1.781539638],
             [1.343808831,3.368360954],
             [3.582294042,4.679179110],
             [2.280362439,2.866990263],
             [7.423436942,4.696522875],
             [5.745051997,3.533989803],
             [9.172168622,2.511101045],
             [7.792783481,3.424088941],
             [7.939820817,0.791637231]
            ]
# 所属类别
raw_data_y = [0,0,0,0,0,1,1,1,1,1]

np.array(raw_data_x)#将数据转化成numpy类型
X_train=np.array(raw_data_x)
y_train=np.array(raw_data_y)
plt.scatter(X_train[y_train==0, 0],X_train[y_train==0, 1], color ='r')
plt.scatter(X_train[y_train==1, 0],X_train[y_train==1, 1], color ='g')
plt.show()
from math import sqrt

手动实现最邻近算法 KNN算法(分类 回归)

手动实现最邻近算法 KNN算法(分类 回归)手动实现最邻近算法 KNN算法(分类 回归)

封装一下上面的代码

from collections import Counter
from math import sqrt
import numpy as np

class KNNClassifier():

    """封装自己KNN算法"""
    def __init__(self, k):
        assert k >= 1, "k 值必须大于1"
        self.k = k
        self._X_train = None # 类似private
        self._y_train = None
    def fit(self, X_train, y_train):
        self._X_train = X_train
        self._y_train = y_train
        return self
    def predict(self, X_predict):
        return np.array([self._predict(x) for x in X_predict])
    # _predict私有方法
    def _predict(self, x_single):
        distances = [sqrt(np.sum((x_train - x_single) ** 2)) for x_train in self._X_train]
        nearst = np.argsort(distances)
        topK_y = [self._y_train[i] for i in nearst[:self.k]]
        votes = Counter(topK_y)
        predict_y = votes.most_common(1)[0][0]
        return predict_y
    def __repr__(self):
        return "KNNClassifier()"

调用试试: 

手动实现最邻近算法 KNN算法(分类 回归)

 

还是用库吧嘿嘿嘿

手动实现最邻近算法 KNN算法(分类 回归)

本文地址:https://blog.csdn.net/qq_39773424/article/details/107121445