《2022年reliefF算法源码.pdf》由会员分享,可在线阅读,更多相关《2022年reliefF算法源码.pdf(10页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、function W = reliefF(Dnolabel,Dlabel,Num,k,typeD,Topn)% % To achieve attribute ranking using reliefF % Dlabel is the array of the label for each record % The data in Dlabel should be format 1,2,3 and so on % Dnolabel is the array of each record without label % Num is loop time for randomly choose in
2、stance % k is the number of nearest hits and miss choosed % Weight is output and it is the ranking of each attribute % typeD is the type of Nolabel data, if it is numerical, typeD=0; % if it is nominal attributes, typeD=1;%y,x=size(Dnolabel); W = zeros(1,Topn);精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢
3、迎下载 名师归纳 - - - - - - - - - -第 1 页,共 10 页 - - - - - - - - - - %calculate the label probabilityLabelRange=max(Dlabel)-min(Dlabel)+1;LabelCount=zeros(1,LabelRange);LabelP=zeros(1,LabelRange);for i=1:y LabelCount(Dlabel(i)=LabelCount(Dlabel(i)+1; endfor j=1:LabelRange LabelP(j)=LabelCount(j)/y;endLabelP
4、%*DistanceArray=zeros(y-1,2);精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 10 页 - - - - - - - - - - %Weight=zeros(1,x);Weight=zeros(x,2);Diff=0; for i=1:x dominoDiff(i) = max(Dnolabel(:,i)- min(Dnolabel(:,i);end%for each loop of random choose instance for NumN=1:Num NumN
5、%randomly choose a instance InstanceId = round(y.*rand(1);while InstanceId=0, InstanceId = round(y.*rand(1);精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 3 页,共 10 页 - - - - - - - - - - end index = 1;%calculate the distance to the choosed instance for each other instance for i=
6、1:yif (i=InstanceId)for j=1:xDistanceArray(index,1)=DistanceArray(index,1)+. (Dnolabel(i,j)-Dnolabel(InstanceId,j)2;endDistanceArray(index,1)=(DistanceArray(index,1)/x); DistanceArray(index,2)=i; index = index+1;end精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 4 页,共 10 页 - - -
7、 - - - - - - - end%sort distance ascending DistanceArray=sortrows(DistanceArray,1); Result=zeros(LabelRange,y); classSize = zeros(1,LabelRange);for i=1:y-1 class = Dlabel(DistanceArray(i,2); classSize(class) = classSize(class)+1; Result(class,classSize(class)=DistanceArray(i,2);end%for each attribut
8、e MissW=0; %the initial value for the part relative Miss in the weight HitW=0; %the initial value for the part relative Hit in the 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 5 页,共 10 页 - - - - - - - - - - weightfor AttributeNum=1:x%calculate the difference of min-max value
9、in this attribute diff = Weight(AttributeNum,1);if (dominoDiff(AttributeNum) Weight(AttributeNum,1)=0; Weight(AttributeNum,2)=AttributeNum;continue ;end%calculate the weight for each attribute for i=1:k %k is the number of nearest neighbours chosen by user idHit = Result(Dlabel(InstanceId),k );%for
10、the Hits精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 6 页,共 10 页 - - - - - - - - - - if (round(typeD)=0) diff = diff - abs( Dnolabel(idHit, AttributeNum) -. Dnolabel(InstanceId, AttributeNum) ). /dominoDiff(AttributeNum)/k; elseif(round(Dnolabel(InstanceId,AttributeNum). =roun
11、d(Dnolabel(idHit, AttributeNum) diff=diff-0;else diff=diff-1;endendend精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 7 页,共 10 页 - - - - - - - - - - for c=1:LabelRangeif (c=Dlabel(InstanceId)continue ;end P=LabelP(c)/(1-LabelP(Dlabel(InstanceId);for i=1:k idMiss = Result(c,i);%f
12、or the misses if (round(typeD)=0) diff = diff + P*abs( Dnolabel(idMiss, AttributeNum) -. Dnolabel(InstanceId, AttributeNum) ). /dominoDiff(AttributeNum)/k; 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 8 页,共 10 页 - - - - - - - - - - elseif(round(Dnolabel(InstanceId,AttributeNu
13、m). =round(Dnolabel(idHit, AttributeNum) diff=diff;else diff=diff+1;endendendend % for m=1:LabelRange Weight(AttributeNum,1)=diff; Weight(AttributeNum,2)=AttributeNum;end % for AttributeNum=1:x精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 9 页,共 10 页 - - - - - - - - - - end % for NumN=1:Num Weight(AttributeNum,1)=diff/Num;Weight(AttributeNum,2)=AttributeNum; W1 = sortrows(Weight);for i = 1:Topn W(i) = W1(x-i+1,2);end精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 10 页,共 10 页 - - - - - - - - - -
限制150内