2、在机器学习中,KNN是不需要训练过程的算法,也就是说,输入样例可以直接调用predict预测结果,训练数据集就是模型。当然这里必须将训练数据和训练标签进行拟合才能形成模型。
import numpy as npfrom math import sqrtfrom collections import Counterclass KNNClassifier: def __init__(self,k): """初始化KNN分类器""" assert k >= 1 """断言判断k的值是否合法""" self.k = k self._X_train = None self._y_train = None def fit(self,X_train,y_train): """根据训练数据集X_train和Y_train训练KNN分类器,形成模型""" assert X_train.shape[0] == y_train.shape[0] """数据和标签的大小必须一样 assert self.k <= X_train.shape[0] """k的值不能超过数据的大小""" self._X_train = X_train self._y_train = y_train return self def predict(self,X_predict): """必须将训练数据集和标签拟合为模型才能进行预测的过程""" assert self._X_train is not None and self._y_train is not None """训练数据和标签不可以是空的""" assert X_predict.shape[1]== self._X_train.shape[1] """待预测数据和训练数据的列(特征个数)必须相同""" y_predict = [self._predict(x) for x in X_predict] return np.array(y_predict) def _predict(self,x): """给定单个待测数据x,返回x的预测数据结果""" assert x.shape[0] == self._X_train.shape[1] """x表示一行数据,即一个数组,那么它的特征数据个数,必须和训练数据相同 distances = [sqrt(np.sum((x_train - x)**2))for x_train in self._X_train] nearest = np.argsort(distances) topk_y = [self._y_train[i] for i in nearest[:self.k]] votes = Counter(topk_y) return votes.most_common(1)[0][0]
from KNN.py import KNNClassifierraw_data_x = [[3.393,2.331], [3.110,1.781], [1.343,3.368], [3.582,4.679], [2.280,2.866], [7.423,4.696], [5.745,3.533], [9.172,2.511], [7.792,3.424], [7.939,0.791]]raw_data_y = [0,0,0,0,0,1,1,1,1,1]X_train = np.array(raw_data_x)y_train = np.array(raw_data_y)x = np.array([9.880,3.555])# 要将x这个矩阵转换成2维的矩阵,一行两列的矩阵X_predict = x.reshape(1,-1)"""1,创建一个对象,设置K的值为6"""knn_clf = KNNClassifier(6)"""2,将训练数据和训练标签融合"""knn_clf.fit(X_train,y_train)"""3,经过2才能跳到这里,传入待预测的数据"""y_predict = knn_clf.predict(X_predict)print(y_predict)