KNN(K Nearest Neighbor)算法的MatLab实现
2013-06-12 21:27
399 查看
有关K近邻的文章有很多,也是非常简单但是又很实用的一种分类方法。
可以参考维基百科:点击打开链接
或者这篇博客:点击打开链接
代码实现:
查找最近K近邻的部分代码:
利用概率求解测试集预测标签部分代码:
应用可以有:
手写体识别、数字验证码识别等。
不同的K的取值对于分界面的选取有很大影响,结果如图:
可以参考维基百科:点击打开链接
或者这篇博客:点击打开链接
代码实现:
function y = knn(X, X_train, y_train, K) %KNN k-Nearest Neighbors Algorithm. % % INPUT: X: testing sample features, P-by-N_test matrix. % X_train: training sample features, P-by-N matrix. % y_train: training sample labels, 1-by-N row vector. % K: the k in k-Nearest Neighbors % % OUTPUT: y : predicted labels, 1-by-N_test row vector. % % Author: Ren Kan [~,N_test] = size(X); predicted_label = zeros(1,N_test); for i=1:N_test [dists, neighbors] = top_K_neighbors(X_train,y_train,X(:,i),K); % calculate the K nearest neighbors and the distances. predicted_label(i) = recog(y_train(neighbors),max(y_train)); % recognize the label of the test vector. end y = predicted_label; end
查找最近K近邻的部分代码:
function [dists,neighbors] = top_K_neighbors( X_train,y_train,X_test,K ) % Author: Ren Kan % Input: % X_test the test vector with P*1 % X_train and y_train are the train data set % K is the K neighbor parameter [~, N_train] = size(X_train); test_mat = repmat(X_test,1,N_train); dist_mat = (X_train-double(test_mat)) .^2; % The distance is the Euclid Distance. dist_array = sum(dist_mat); [dists, neighbors] = sort(dist_array); % The neighbors are the index of top K nearest points. dists = dists(1:K); neighbors = neighbors(1:K); end
利用概率求解测试集预测标签部分代码:
function result = recog( K_labels,class_num ) %RECOG Summary of this function goes here % Author: Ren Kan [~,K] = size(K_labels); class_count = zeros(1,class_num+1); for i=1:K class_index = K_labels(i)+1; % +1 is to avoid the 0 index reference. class_count(class_index) = class_count(class_index) + 1; end [~,result] = max(class_count); result = result - 1; % Do not forget -1 !!! end
应用可以有:
手写体识别、数字验证码识别等。
不同的K的取值对于分界面的选取有很大影响,结果如图:
相关文章推荐
- KNN的matlab实现算法
- KNN(K-Nearest Neighbor)算法Matlab实现
- 机器学习:KNN算法(MATLAB实现)
- KNN的matlab实现算法(转载)
- KNN算法matlab代码实现
- 数据挖掘-基于贝叶斯算法及KNN算法的newsgroup18828文档分类器的JAVA实现(上)
- 1.4 KNN算法学习——KNN算法的实现与封装
- MATLAB智能算法实现 (一)
- 双边滤波(bilateral filter)快速算法 matlab实现代码
- 文本挖掘——基于TF-IDF的KNN分类算法实现
- KNN分类算法及python代码实现
- MATLAB实现寻路、布线、迷宫的递归深度优先遍历算法_动画演示
- 【机器学习 3】KNN算法实现梳理- Be based on“约会对象”、“手写识别”
- KNN分类算法实现手写数字识别
- Fisher线性判别与感知器算法Matlab实现
- 基于欧几里德距离的K最近邻(KNN)算法的实现(JAVA版)
- 图论--Matlab实现最短路径算法
- PCA降维算法总结以及matlab实现PCA(个人的一点理解)
- PCA算法学习_2(PCA理论的matlab实现)
- KNN的matlab简单实现代码