K-S算法选取训练集和测试集
简单的一个算法,查了一下都让人下载收费,就是很烦。K-S选取训练集的原理类似于挑女朋友,先选取两个离得最远的异地恋先谈着,然后觉得太远了,在找一个新的,进行比对,比对结果觉得挑的这个更好就进行替换,如此反复迭代选出你心目中最喜欢的几个女朋友(训练集),剩下的就是备胎了(测试集)
其他的不多说,直接贴代码吧
function [XSelected,XRest,vSelectedRowIndex]=ks(X,Num)
% ks selects the samples XSelected which uniformly distributed in the exprimental data X’s space
% Input
% X:the matrix of the sample spectra
% Num:the number of the sample spectra you want select
% Output
% XSelected:the sample spectras was selected from the X
% XRest:the sample spectras remain int the X after select
% vSelectedRowIndex:the row index of the selected sample in the X matrix
% Programmer: xiaoyi_1994 XMU
%% start of the kennard-stone step one
X=xlsread(‘X.xlsx’);%obtain the data
[nR,nC]=size(X); % obtain the size of the X matrix
mDistance=zeros(nR,nR); %dim a matrix for the distance storage
vAll of Sample=1:nR;
for i=1:nR-1
vRX=X(i,:); % 获取X的一行数据
for j=i+1:nR
vRX1=X(j,:); % 获得X中的另一行数据
mDistance(i,j)=norm(vRX-vRX1); % 计算欧氏距离
end
end
[vMax,vIndex Of mDistance]=max(mDistance);
[nMax,nIndex of vMax]=max(vMax);
vSelectedSample(1)=nIndex of vMax;
vSelectedSample(2)=vIndex Of mDistance(nIndex of vMax);
% end of the kennard-stone step one
%% start of the kennard-stone step two
for i=3:Num
vNotSelectedSample=setdiff(vAll of Sample,vSelectedSample);
vMinDistance=zeros(1,nR-i + 1);
for j=1:(nR-i+1)
nIndex of NotSelected=vNotSelectedSample(j);
vDistanceNew = zeros(1,i-1);
for k=1:(i-1)
nIndex of Selected=vSelectedSample(k);
if(nIndex of Selected<=nIndex of NotSelected)
vDistanceNew(k)=mDistance(nIndex of Selected,nIndex of NotSelected);
else
vDistanceNew(k)=mDistance(nIndex of NotSelected,nIndex of Selected);
end
end
vMinDistance(j)=min(vDistanceNew);
end
[nUseless,nIndex of vMinDistance]=max(vMinDistance);
vSelectedSample(i)=vNotSelectedSample(nIndex of vMinDistance);
end
%%%%% end of the kennard-stone step two
%% start of export the result
vSelectedRowIndex=vSelectedSample;
for i=1:length(vSelectedSample)
XSelected(i,:)=X(vSelectedSample(i)😅; %训练集数据
end
vNotSelectedSample=setdiff(vAll of Sample,vSelectedSample);
for i=1:length(vNotSelectedSample)
XRest(i,:)=X(vNotSelectedSample(i)😅; %预测集数据
end
%%%%% end of export the result