% cvKnn - K-Nearest Neighbor classification
%算距离,找邻居,做分类
% Synopsis
% [Class] = cvKnn(X, Proto, ProtoClass, [K], [distFunc])
% N, ProtoClass, K, d
% Description
% K-Nearest Neighbor classification
%
% Inputs ([]s are optional)
% (matrix) X D x N matrix representing column classifiee vectors
% where D is the number of dimensions and N is the
% number of vectors.
% (matrix) Proto D x P matrix representing column prototype vectors
% where D is the number of dimensions and P is the
% number of vectors.
% (vector) ProtoClass
% 1 x P vector containing class lables for prototype
% vectors.
% (scalar) [K = 1] K-NN's K. Search K nearest neighbors.
% (func) [distFunc = @cvEucdist]
% A function handle for distance measure. The function
% must have two arguments for matrix X and Y. See
% cvEucdist.m (Euclidean distance) as a reference.
%
% Outputs ([]s are optional)
% (vector) Class 1 x N vector containing classified class labels
% for X. Class(n) is the class id for X(:,n).
% (matrix) [Rank] Available only for NN (K = 1) now.
% nClass x N vector containing ranking class labels
% for X. Rank(1,n) is the 1st candidate which is
% the same with Class(n), Rank(2,n) is the 2nd
% candidate, Rank(3,n) is the 3rd, and so on.
%
% See also
% cvEucdist, cvMahaldist
% Authors
% Naotoshi Seo <sonots(at)sonots.com>
%
% License
% The program is free to use for non-commercial academic purposes,
% but for course works, you must understand what is going inside to use.
% The program can be used, modified, or re-distributed for any purposes
% if you or one of your group understand codes (the one must come to
% court if court cases occur.) Please contact the authors if you are
% interested in using the program without meeting the above conditions.
%
% Changes
% 04/01/2005 First Edition
function [Class, Rank] = cvKnn(N, ProtoClass, K, d)
if ~exist('K', 'var') || isempty(K)
K = 1;
end
% if ~exist('distFunc', 'var') || isempty(distFunc)
% distFunc = @distM;
% end
% if size(X, 1) ~= size(Proto, 1)
% error('Dimensions of classifiee vectors and prototype vectors do not match.');
% end
% [D, N] = size(X);
% Calculate euclidean distances between classifiees and prototypes
% d = distFunc(X, Proto);
if K == 1, % sort distances only if K>1
[mini, IndexProto] = min(d, [], 1); % 2 是取每一行的最小值(若为1就是取每一列的最小值)(第一项存最小值,第二项存索引,最小值所在位置)
Class = ProtoClass(IndexProto);%(把训练集的每一行的最小值放进Class里面)
if nargout == 2, % instance indices in similarity descending order(按相似度降序的案例索引)输出参量为两个
[sorted, ind] = sort(d'); % PxN(sort(x),默认为按列升序排序)(d'也就是d的每一行的最小值)
RankIndex = ProtoClass(ind); %,e.g., [2 1 2 3 1 5 4 1 2]'
% conv into, e.g., [2 1 3 5 4]'
for n = 1:N
[ClassLabel, ind] = unique(RankIndex(:,n),'first');
[sorted, ind] = sort(ind);
Rank(:,n) = ClassLabel(ind);
end
end
else
[sorted, IndexProto] = sort(d); % PxN(d的每一行的最小值及其位置存在矩阵里面)
% K closest
IndexProto = IndexProto(1:K,:);%(第1个到第k个的索引位置)
KnnClass = ProtoClass(IndexProto);%(这k个训练集)
% Find all class labels
ClassLabel = unique(ProtoClass);
nClass = length(ClassLabel);
for i = 1:nClass
ClassCounter(i,:) = sum(KnnClass == ClassLabel(i));
end
[maxi, winnerLabelIndex] = max(ClassCounter, [], 1); % 1 == col
% Future Work: Handle ties somehow
Class = ClassLabel(winnerLabelIndex);
end
没有合适的资源?快使用搜索试试~ 我知道了~
KML.zip_度量学习
共10个文件
m:8个
mat:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 14 浏览量
2022-07-15
12:27:14
上传
评论
收藏 8.65MB ZIP 举报
温馨提示
本压缩包包含几何度量平均度量学习方法代码的核化版本,内含所有子函数。
资源详情
资源评论
资源推荐
收起资源包目录
KML.zip (10个子文件)
KML
kml.m 408B
distM.m 311B
kgmml.m 529B
crossvalidate.m 3KB
label.mat 247B
imain.m 838B
caldistance.m 578B
xiaodao.mat 8.65MB
cvKnn.m 4KB
kernelsim.m 332B
共 10 条
- 1
钱亚锋
- 粉丝: 101
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- STM32参考资料STM32 固件库使用参考资料
- java智能停车后台管理系统源码数据库 MySQL源码类型 WebForm
- STM32参考资料STM32中断优先级与相关使用概念
- Linux环境下,关于C++静态库的封装和调用代码
- STM32参考资料STM32F10x常见应用解析
- java面试视频资源微服务架构之Spring Cloud Eureka 场景分析与实战
- java面试视频资源探索JVM底层奥秘ClassLoader源码分析与案例讲解
- java面试视频资源锁分布式锁无锁实战全局性ID
- java基于SSM的酒店管理系统源码数据库 MySQL源码类型 WebForm
- java面试视频资源JAVA并发编程之多线程并发同步业务场景与解决方案
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0