⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 knearestestimate.m

📁 脱机手写体识别Matlab源程序 包括特征提取、bayes分类器、K近邻分类及最近邻分类。 TestScriptRecognition.m:测试代码 ScriptFeaExtract.m :
💻 M
字号:
% k nearest neighbor estimation 
function [KNearest]=KNearestEstimate(trainX,trainY,testX,k)
trainCnt=size(trainX,2);         % 训练样本数量
testCnt=size(testX,2);           % 测试样本数量

classLabMin=min(trainY);         % 类标最小值
classLabMax=max(trainY);         % 类标最大值 
KNearest=zeros(1,testCnt);       % kn最近邻估计结果
for i=1:testCnt
    curSample=testX(:,i);
    dist=zeros(1,trainCnt);
    for j=1:trainCnt             % 计算与每个测试样本的欧式距离
        dist(j)=sqrt(sum((curSample-trainX(:,j)).^2));
    end
    [distSeq indexSeq]=sort(dist);
    estKSample=indexSeq(1:k);    % 取距离最近的前k个样本
    
    maxKj=-1;
    for j=classLabMin:classLabMax       
        curKj=length(find(trainY(estKSample)==j));% 计算每一类别的数量
        if(curKj>maxKj)
            maxKj=curKj;
            KNearest(i)=j;       % 取类别数量最多的类别标号
        end
    end
end
    





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -