资源简介
可以通过使用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
相关资源
- 遗传算法优化pid控制器参数的matlab程
- 隐式广义预测控制matlab程序
- JONSWAP海谱模型
- matlab 人工鱼群求解TSP
- MQAM和MPSK误符号率曲线绘制
- 最好用的串口调试助手_MATLAB
- Matlab小波包分解函数
- Matlab自动识别串口设备名称
- 基于峭度的ICA matlab代码程序
-
Simuli
nk电池仿真模型 - RLC电路matlab仿真m文件
- matlab矩阵运算
- 最大匹配matlab程序
- 凸轮仿真加工MATLAB程序
- LCE OCE GCEmatlab代码用于图像分割精度评
- 连续粒子群算法的matlab程序
- 窗口傅里叶正反变换matlab程序
- 改进的Bradley二值化matlab程序
- 改进的bernsen二值化算法
- matlab_webrtc_fullaec.m
- 边缘检测的matlab实现代码
- A*算法最短路径万能通用matlab代码
- 蚁群算法最短路径万能matlab源代码
- MATLAB蚁群算法ACA最短路径-注释完整
- 卡尔曼平滑
- matlab强化学习平衡杆代码
- matlab_基于QPSK的ML检测算法
- 模糊综合评判的matlab实现
- 三相全控整流电路matlab仿真
- 带转矩内环的转速、磁链闭环矢量控
评论
共有 条评论