-
分享一下最近学的KNN算法
-
K近邻算法简单一点来说就是分类算法,测试点与相邻训练数据K个点,根据点与点之间的距离判断测试点的类别
- 代码如下
注意:训练数据X_train和Y_train是对应关联的 : X_train.shape[0] == Y_train.shape[0]
-
import numpy as np form math import sqrt form collections import Counter #自己封装的KNN方法 class KNNClassifier: def __init__(self, K): """初始化KNN分类器""" assert k >= 1, "K must be valid" 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], "the size of X_train must be to the size of Y_train" assert self.K <= X_train.shape[0], "the size of X_train must be at least K " self._X_train = X_train self._Y_train = Y_train return self def predict(self, X_predict): """给定待预测数据集X_predict, 返回表示X_predict的结果向量""" assert self._X_train is not None and self._Y_train is not None, "must fit before predict!" assert X_predict.shape[1] == self._X_train.shape[1], "the feature number of X_predict must be equal to X_train" 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], "the feature number of x must be equal to X_train" distances = [sqrt(np.sum((X_train - x) ** 2)) for X_train in self._X_train] #排序,返回的是下标的数组 nearest = np.argsort(distances) #X_train和Y_train是一一对应的关系,在这里使用下标来关联 tooK_y = [self._Y_train[i] for i in nearest[:self.K]] votes = Counter(tooK_y) return votes.most_common(1)[0][0] def __repr__(self): return "KNN(k=%d)" % self.K #用法 my_knn_clf = KNNClassifier(k=3) #X_train,Y_train是训练数据集,在这里没有给出 my_knn_clf.fit(X_train, Y_train) #x是训练数据,可以是多维数据也可以是单个向量,同样在这里没有给出 y_predict = my_knn_clf.predict(x) print y_predict
同样可以在Jupyter Notebook中运行
-
在这里给出X_train和Y_train数据
-
运行,返回结果
-
上述就是KNN算法原理》》
-
也可以在sklearn-learn中直接使用KNN算法
from sklearn.neighbors import KNeighborsClassifier
KNN_classifier = KNeighborsClassifier(n_neighbors = 6)
KNN_classifier.fit(X_train, Y_train)
KNN_classifier.predict(x)
ENDING>>>>