co training matlab,MATLAB中,knn函数中的training什么意思 ?

3f387a6397fcb0daa33b0033b783ae60.png

精慕HU

function [ccr,pgroupt]=knnt(x,group,K,dist,xt,groupt)%#%# AIM: to classify test set objects or unknown objects with the%#K Nearest Neighbour method%#%# PRINCIPLE: KNN is a supervised, deterministic, non-parametric%#classification method. It uses the majority rule to%#assign new objects to a class.%#It is assumed that the number of objects in each class%#is similar.%# There are no assumptions about the data distribution and%#the variance-covariance matrices of each class.%#There is no limitation of the number of variables when%#the Euclidean distance is used.%#However, when the correlation coefficient is used, the%#number of variables must be larger than 1.%#Ref: Massart D. L., Vandeginste B. G. M., Deming S. N.,%#Michotte Y. and Kaufman L., Chemometrics: a textbook,%#Chapter 23, 395-397, Elsevier Science Publishers B. V.,%#Amsterdam 1988.%#%# INPUT:x: (mxn) data matrix with m objects and n variables,%#containing samples of several classes (training set)%#group: (mx1) column vector labelling the m objects from the%#training set%# K:integer, number of nearest neighbours%# dist:integer,%#= 1, Euclidean distance%#= 2, Correlation coefficient, (No. of variables >1)%# xt: (mtxn) data matrix with mt objects and n variables%#(test set or unknowns)%#groupt: (mtx1) column vector labelling the mt objects from%#the test set%#--> if the new objects are unknown, input [].%#%# OUTPUT:ccr:scalar, correct classification rate%# pgroupt:row vector, predicted class label for the test set%#0 means that the object is not classified to any%#class%#%# SUBROUTINES: sortlab.m: sorts the group label vector into classes%#%# AUTHOR: Wen Wu%#Copyright(c) 1997 for ChemoAc%# FABI, Vrije Universiteit Brussel%# Laarbeeklaan 103 1090 Jette%#%# VERSION: 1.1 (28/02/1998)%#%# TEST: Andrea Candolfi%#function [ccr,pgroupt]=knnt(x,group,K,dist,xt,groupt);if nargin==5, groupt=[]; end % for unknown objectsdistance=dist; clear dist % change variableif size(group,1)>1,group=group'; % change column vector into row vectorgroupt=groupt'; % change column vector into row vectorend;[m,n]=size(x); % size of the training setif distance==2 & n<2, error('Number of variables must > 1'),end% to check the number of variables when using correlation coefficient[mt,n]=size(xt); % size of the test setdis=zeros(mt,m);% initial values for the distance (matrix of zeros)% Calculation of the distance for each test set objectfor i=1:mtfor j=1:m % between each training set object and each test set objectif distance==1dis(i,j)=(xt(i,:)-x(j,:))*(xt(i,:)-x(j,:))';% Euclidian distanceelser=corrcoef(xt(i,:)',x(j,:)');% Correlation coefficient matrixr=r(1,2);% Correlation coefficientdis(i,j)=1-r*r; % 1 - the power of correlation coefficientendendend% Finding of the nearest neighbourslab=zeros(1,mt);% initial values of labfor i=1:mt% for each test object[a,b]=sort(dis(i,:));% sort distancesb=b(find(a<=a(K)));% to find the nearest neighbours indicesb=group(b);% the nearest neighbours objects[ng,lgroup]=sortlab(b);% calculate the number of objects from each class in the nearest neighboursa=find(ng==max(ng));% find the class with the maximum number of objectsif length(a)==1% only one classlab(i)=lgroup(a);% class labelelselab(i)=0;% more than one classendend% Calculation of the success rateif ~isempty(groupt)dif=groupt-lab;% difference between predicted class label and known class labelccr=sum(dif==0)/mt;% success rateendpgroupt=lab;% the output vector

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值