资源简介
可以通过使用KNN分类器进行图片分类,KNN分类器完整的matlab代码。

代码片段和文件信息
function results = knn_classify(train_data test_data num_neighbors train_labels)
% classify test_data using KNN classifier established from train_data
% Input:
% train_data - training data TRAIN_NUM x D matrix
% test_data - testing data TEST_NUM x D matrix
% num_neighbors - number of nearest neighbors
% train_labels - training labels TRAIN_NUM x 1 matrix
% Output:
% results - labels for testing data TEST_NUM x 1 array
[TRAIN_NUM D] = size(train_data);
[TEST_NUM D2] = size(test_data);
results = zeros(TEST_NUM 1);
for i = 1:TEST_NUM
[dists neighbors] = find_top_K_neighbors(train_data test_data(i:) num_neighbors);
results(i) = recog(train_labels(neighbors) max(train_labels));
end
%------------------------------subfunction--------------------------------%
function [dists neighbors] = find_top_K_neighbors(train_data test_sample K)
% find the top K nearest neighbors in the train_data for test_sample
% Input:
% train_data - training data N x D matrix
% test_sample - test_sample 1 x D array
% K - number of neighbors
% Output:
% dist - least K distance
% neighbors - K nearest neighbors 1 x K array
[N D] = size(train_data);
[dummy D2] = size(test_sample);
test_matrix = repmat(test_sample N 1); % N by D
dist_mat = (train_data - test_matrix) .^ 2; % N by D
dist_array = sum(dist_mat‘); % 1 by N
[dists neighbors] = sort(dist_array);
dists = dists(1:K);
neighbors = neighbors(1:K);
%--------------------------------subfunction------------------------------%
function label = recog(neighbor_labels num_class)
% find the label for the current test sample with neighbor_labels
% Input:
% neighbor_labels - labels for K neighors of the current
% test sample
% num_class - number of class
% Output:
% label - label for the current test sample
num_neighbors = length(neighbor_labels);
cnt_labels = zeros(num_class 1);
for i = 1:num_neighbors
cnt_labels(neighbor_labels(i)) = cnt_labels(neighbor_labels(i)) + 1;
end
[dummy label] = max(cnt_labels);%Y是返回最大值的,I是返回最大值的位置的。
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-07-18 16:01 knn\
文件 2448 2013-06-28 09:26 knn\knn_classify.m
文件 2253 2013-07-18 15:47 knn\main.m
- 上一篇:遗传算法优化pid控制器参数的matlab程序
- 下一篇:模糊免疫PID
相关资源
- 串行级联cpm系统MATLAB仿真
- matlab_OFDM调制解调(来自剑桥大学)
- Matlab路面裂缝识别69319
- 高灵敏度GPS接收机MATLAB仿真,附捕获
- 基于MATLAB的质点弹道计算与外弹道优
- 阵列天线的matlab仿真
- MATLAB 经典程序源代码大全
- MATLAB小波软阈值去噪代码33473
- 天线阵的波束形成在MATLAB仿真程序及
- 非线性SVM算法-matlab实现
- 《MATLAB 智能算法超级学习手册》-程序
- 组合导航matlab程序
- 读取txt文件内容matlab代码实现
- Matlab实现基于相关的模板匹配程序
- matlab优化工具箱讲解
- 基于MATLAB的快速傅里叶变换
- 光纤传输中的分布傅立叶算法matlab实
- 基于matlab的图像处理源程序
- matlab 椭圆拟合程序
- 算术编码解码matlab源代码
- optical_flow 光流法 matlab 实现程序
- 引导图像滤波器 Matlab实现
- 分形几何中一些经典图形的Matlab画法
- OFDM系统MATLAB仿真代码
- SVM工具箱(matlab中运行)
- 图像小波变换MatLab源代码
- LU分解的MATLAB实现
- 冈萨雷斯数字图像处理matlab版(第三
- 替代数据法的matlab程序
- 用matlab实现的多站定位系统性能仿真
评论
共有 条评论