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

📄 da_pcr.m

📁 王小平《遗传算法——理论、应用与软件实现》随书光盘
💻 M
字号:
%
% mv_pcr
%
% Principal Component Regression entry point
%

%
% Clear the screen
%

da_front;
drawnow;
clear impact py vy;

%
% Make sure that none of the variables have a zero
% standard deviation
%
s=std(data);
[D L]=size(s);
for i = 1 : L
	if s(i)==0
		include_var(i)=0;
		output_var(i)=0;
	end
end

%
% Draw a set of axes
%
figure(w1);
set(w1,'NumberTitle','off','Name','Principal Components Regression (PCR)');

ax1=axes(...
	'Units','pixels',...
	'Position',[60 230 200 180],...
	'Box','on',...
	'Color',[0 0 0],...
	'Visible','on');
Xlabel('Sample number');
Ylabel('Output');

text2=uicontrol(w1,...
	'Style','text',...
	'Position',[50 415 150 15],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Regression results:');

text1=uicontrol(w1,...
	'Style','text',...
	'Position',[50 160 100 15],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Contributions:');

ax2=axes(...
	'Units','pixels',...
	'Position',[60 80 200 75],...
	'Box','on',...
	'Color',[0 0 0],...
	'Visible','on');
Xlabel('Variable number');
Ylabel('Contibution');


%
% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%
% Now have three mutually exclusive radio buttons for
% optimum regression options
%

pcr_rad1=uicontrol(w1,...
	'Style','radio',...
	'Position',[270 300 210 20],...
	'String','Include remaining components:',...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'CallBack',[...
		'set(pcr_rad1,''Value'',1);',...
		'set(pcr_rad3,''Value'',0);']);

pcr_rad3=uicontrol(w1,...
	'Style','radio',...
	'Position',[270 270 300 20],...
	'String','Optimum number of components',...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'CallBack',[...
		'set(pcr_rad1,''Value'',0);',...
		'set(pcr_rad3,''Value'',1);']);


set(pcr_rad1,'Value',0);
set(pcr_rad3,'Value',1);

%
% Print title for these options
%
text1=uicontrol(w1,...
	'Style','text',...
	'Position',[350 330 150 15],...
	'BackGroundColor',[1 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Regression options...');


%
% Do a slider for the "include remaining components" option
%

sl2=uicontrol(w1,...
	'Style','slider',...
	'Position',[480 300 100 20],...
	'Min',1,...
	'Max',sum(include_var)-sum(output_var),...
	'Value',sum(include_var)-sum(output_var),...
	'CallBack','set(sl2_current,''String'',num2str(ceil(get(sl2,''Value''))))');

sl2_max=uicontrol(w1,...
	'Style','text',...
	'Position',[585 300 20 15],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String',num2str(sum(include_var)-sum(output_var)));

sl2_current=uicontrol(w1,...
	'Style','text',...
	'Position',[540 320 20 15],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String',num2str(ceil(get(sl2,'Value'))));


%
% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

%
% CALCULATE button
%
pca_but1=uicontrol(w1,...
	'Style','push',...
	'Position',[260 10 100 20],...
	'String','CALCULATE',...
	'CallBack',[...
		'da_pcrgo;',...
		'axes(ax1);',...
		'plot([vy(:,1) py(:,1)]);',...
		'[D L]=size(py);',...
		'set(ax1,''Xlim'',[0 D]);',...
		'Xlabel(''Sample number'');',...
		'Ylabel(''Output'');',...
		'axes(ax2);',...
		'bar(impact(1,:));',...
		'set(ax2,''Xlim'',[0 sum(include_var)-1]);',...
		'Xlabel(''Variable number'');',...
		'Ylabel(''Contribution'');']);


%
% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%
% Options for data splitting
%

text2=uicontrol(w1,...
	'Style','text',...
	'Position',[320 220 200 15],...
	'BackGroundColor',[1 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Training/Validation splits...');

%
% Percentage straight split
%
pcr_rad4=uicontrol(w1,...
	'Style','radio',...
	'Position',[270 190 150 20],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Percentage partition',...
	'CallBack',[
		'set(pcr_rad4,''Value'',1);',...
		'set(pcr_rad5,''Value'',0);',...
		'set(pcr_rad6,''Value'',0);',...
		'set(pcr_rad7,''Value'',0);']);


%
% Slider for this:
%
	pcr_rad4sl=uicontrol(w1,...
		'Style','slider',...
		'Position',[420 190 100 20],...
		'Min',0,...
		'Max',100,...
		'Value',50,...
		'BackgroundColor',[0 0 0],...
		'ForeGroundColor',[1 1 1],...
		'String','Percentage',...
		'CallBack','set(pcr_rad4sl_current,''String'',[num2str(ceil(get(pcr_rad4sl,''Value''))) '' % train''])');
	pcr_rad4sl_current=uicontrol(w1,...
		'Style','text',...
		'Position',[530 190 70 15],...
		'BackGroundColor',[1 0 0],...
		'ForegroundColor',[1 1 1],...
		'String',[num2str(ceil(get(pcr_rad4sl,'Value'))) ' % train']);

	
%
% Randomly assign percentage
%
pcr_rad5=uicontrol(w1,...
	'Style','radio',...
	'Position',[270 160 150 20],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Randomly assign',...
	'CallBack',[
		'set(pcr_rad4,''Value'',0);',...
		'set(pcr_rad5,''Value'',1);',...
		'set(pcr_rad6,''Value'',0);',...
		'set(pcr_rad7,''Value'',0);']);

set(pcr_rad5,'enable','off');

%
% Slider for this
%
	pcr_rad5sl=uicontrol(w1,...
		'Style','slider',...
		'Position',[420 160 100 20],...
		'Min',0,...
		'Max',100,...
		'Value',50,...
		'BackgroundColor',[0 0 0],...
		'ForeGroundColor',[1 1 1],...
		'String','Percentage',...
		'CallBack','set(pcr_rad5sl_current,''String'',[num2str(ceil(get(pcr_rad5sl,''Value''))) '' % train''])');
	pcr_rad5sl_current=uicontrol(w1,...
		'Style','text',...
		'Position',[530 160 70 15],...
		'BackGroundColor',[1 0 0],...
		'ForegroundColor',[1 1 1],...
		'String',[num2str(ceil(get(pcr_rad5sl,'Value'))) ' % train']);


%
% Multiple block splitting
%
pcr_rad6=uicontrol(w1,...
	'Style','radio',...
	'Position',[270 130 150 20],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Multiple block split',...
	'CallBack',[
		'set(pcr_rad4,''Value'',0);',...
		'set(pcr_rad5,''Value'',0);',...
		'set(pcr_rad6,''Value'',1);',...
		'set(pcr_rad7,''Value'',0);']);
%
% Slider for this
%
[D L]=size(data);
	pcr_rad6sl=uicontrol(w1,...
		'Style','slider',...
		'Position',[420 130 100 20],...
		'Min',4,...
		'Max',ceil(D/8),...
		'Value',ceil(D/16),...
		'BackgroundColor',[0 0 0],...
		'ForeGroundColor',[1 1 1],...
		'String','Percentage',...
		'CallBack','set(pcr_rad6sl_current,''String'',[num2str(ceil(get(pcr_rad6sl,''Value''))) '' blocks''])');
	pcr_rad6sl_current=uicontrol(w1,...
		'Style','text',...
		'Position',[530 130 70 15],...
		'BackGroundColor',[1 0 0],...
		'ForegroundColor',[1 1 1],...
		'String',[num2str(ceil(get(pcr_rad6sl,'Value'))) ' blocks']);





pcr_rad7=uicontrol(w1,...
	'Style','radio',...
	'Position',[270 100 200 20],...
	'BackGroundColor',[0 0 0],...
	'ForeGroundColor',[1 1 1],...
	'String','Split data alternately',...
	'CallBack',[
		'set(pcr_rad4,''Value'',0);',...
		'set(pcr_rad5,''Value'',0);',...
		'set(pcr_rad6,''Value'',0);',...
		'set(pcr_rad7,''Value'',1);']);

%
% Set so that the default is percentage split
%
set(pcr_rad4,'Value',1);
set(pcr_rad5,'Value',0);
set(pcr_rad6,'Value',0);
set(pcr_rad7,'Value',0);


%
% Do results box
%
drawnow;
box1=uicontrol(w1,...
	'Style','frame',...
	'Position',[269 359 302 49],...
	'BackGroundColor',[0 0 1]);

%
% If there is more than one output variable, draw a slider
%
old_sl1_value=1;
if sum(output_var & include_var) > 1
	text4=da_text(w1,40,30,200,15,'Viewing output variable 1',[1 1 1],[0 0 1]);
	text5=da_text(w1,10,10,60,15,'Output:',[1 1 1],[0 0 0]);
	sl1_value=1;
	old_sl1_value=1;
	sl1=uicontrol(w1,...
		'style','slider',...
		'position',[75 10 100 20],...
		'Max',sum(output_var),...
		'Min',1,...
		'Value',1,...
		'CallBack',[...
			'sl1_value=ceil(get(sl1,''value''));',...
			'if sl1_value ~= old_sl1_value;',...
				'old_sl1_value=sl1_value;',...
				'set(sl1,''value'',ceil(get(sl1,''value'')));',...
				'set(text4,''string'',[''Viewing output Variable '' num2str(ceil(get(sl1,''value'')))]);',...
				'[D L]=size(vy);',...
				'e=((sse(vy(:,sl1_value),py(:,sl1_value)).^0.5)/D)/my(sl1_value);',...
				'c=corrcoef(vy(:,sl1_value),py(:,sl1_value));',...
				'c=c(2,1);',...
				'set(text1,''string'',[''Error =  '' num2str(e)]);',...
				'set(text3,''string'',[''Correlation =  '' num2str(c)]);',...
				'axes(ax1);',...
				'plot([vy(:,sl1_value) py(:,sl1_value)]);',...
				'Xlabel(''Sample number'');',...
				'Ylabel(''Output'');',...
				'set(ax1,''xlim'',[0 D]);',...
				'axes(ax2);',...
				'bar(impact);',...
				'Xlabel(''Variable number'');',...
				'Ylabel(''Contribution'');',...
			'end;']);
	set(text4,'visible','off');
	set(text5,'visible','off');
	set(sl1,'enable','off','visible','off');
end

⌨️ 快捷键说明

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