栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

kennard-stone(K-S算法)选取训练集和测试集样本数据

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

kennard-stone(K-S算法)选取训练集和测试集样本数据

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/650146.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号