?? demosvm.asv
字號(hào):
function varargout = demosvm(varargin)
% 非線(xiàn)性模式分析模型.
%
% 說(shuō)明:
% demosvm實(shí)現(xiàn)多類(lèi)數(shù)據(jù)分類(lèi)
% 用戶(hù)可以裝載數(shù)據(jù),也可以手動(dòng)創(chuàng)建
%
% 使用的訓(xùn)練算法:
% - OAA算法
% - OAO 算法
% - BSVM2算法
% 使用的核函數(shù)
% -linear(線(xiàn)性核函數(shù))
% - rbf(徑向基核函數(shù))
% - poly(多項(xiàng)式核函數(shù))
% - sigmoid(S型核函數(shù))
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @demosvm_OpeningFcn, ...
'gui_OutputFcn', @demosvm_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% 系統(tǒng)初始化代碼。
% 系統(tǒng)可視化。
function demosvm_OpeningFcn(hObject, eventdata, handles, varargin)
%demosvm中使用以下結(jié)構(gòu)體,來(lái)實(shí)現(xiàn)圖形窗口上的各個(gè)控件的數(shù)據(jù)傳送,進(jìn)而實(shí)現(xiàn)各個(gè)控件的功能。
data=struct(...
'pushbutton1', handles.pushbutton1,... %創(chuàng)建數(shù)據(jù)
'pushbutton2', handles.pushbutton2,... %裝載數(shù)據(jù)
'pushbutton3', handles.pushbutton3,... %重置數(shù)據(jù)
'pushbutton4', handles.pushbutton4,... %訓(xùn)練SVM
'pushbutton5', handles.pushbutton5,... %關(guān)閉
'pushbutton6', handles.pushbutton6,... %圖象導(dǎo)出
'pushbutton7', handles.pushbutton7,... %計(jì)算SV
'pushbutton8', handles.pushbutton8,... %決策邊界
'popupmenu1', handles.popupmenu1,...%訓(xùn)練算法
'popupmenu2', handles.popupmenu2,...%核函數(shù)
'popupmenu3', handles.popupmenu2,...%C參數(shù)
'edit1', handles.edit1,...% 注釋窗口
'axes1', handles.axes1); %圖形顯示坐標(biāo)軸
set(gcf ,'UserData',data );% 將圖形窗口顯示在桌面上
set(gcf,'Visible','on');
drawnow;
handles.output = hObject;
guidata(hObject, handles);
function varargout = demosvm_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% 創(chuàng)建數(shù)據(jù)回調(diào)函數(shù)
function pushbutton1_Callback(hObject, eventdata, handles)
n=5;
createdata('finite',n,'created',gcf);
% 裝載數(shù)據(jù)回調(diào)函數(shù)
function pushbutton2_Callback(hObject, eventdata, handles)
h = waitbar(0,'請(qǐng)等待...');%等待條行窗口
for i=1:100,
waitbar(i/100)
end
close(h)
data=get(gcf,'UserData');
[name,path]=uigetfile('*.mat','Open file');
if name~=0,
file.pathname=strcat(path,name);
file.path=path;
file.name=name;
if check2ddata( file.pathname )
set(data.pushbutton2,'UserData',file);
cla;
data=loadsets(gcf); % 裝載結(jié)構(gòu)型數(shù)據(jù)
set(gcf,'UserData',data);
set(handles.edit1,'String','數(shù)據(jù)已經(jīng)裝載 !');
else
errordlg('This file does not contain required data.','Bad file','modal');
end
end
%重置數(shù)據(jù)回調(diào)函數(shù)
function pushbutton3_Callback(hObject, eventdata, handles)
cla;
set(handles.edit1,'String','數(shù)據(jù)已經(jīng)清除,請(qǐng)重新裝載 !');
% 訓(xùn)練SVM回調(diào)函數(shù)----軟件核心部分
function pushbutton4_Callback(hObject, eventdata, handles)
ker_inx = get( handles.popupmenu2, 'Value' ); %獲取核函數(shù)信息
if ker_inx == 1;
ker = 'linear';
set( handles.edit3,'Enable','off');
elseif ker_inx == 2;
ker = 'rbf';
set( handles.edit3,'Enable','on');
elseif ker_inx == 3;
ker = 'poly';
set( handles.edit3,'Enable','on');
else
ker = 'sigmoid';
set( handles.edit3,'Enable','on');
end
C_inx = get( handles.popupmenu3, 'Value' ); %獲取C參數(shù)信息
if C_inx == 1;
switch get( handles.popupmenu1, 'Value' )
case 1
data=get(handles.axes1,'userdata');
options.solver = 'smo'; % 使用 SMO solver
options.ker = ker; % 使用核函數(shù)
options.arg = 1; % kernel argument
options.C = C; %調(diào)整參數(shù)
T1=cputime;
model = oaasvm(data,options ); % 訓(xùn)練
T=cputime-T1;%計(jì)算訓(xùn)練所用時(shí)間
set(handles.pushbutton6,'userdata',T);%將時(shí)間變量傳遞出去
%顯示圖形
axes(handles.axes1);cla;
ppatterns(data);
D=struct('solver',options.solver,'ker',options.ker,'arg',options.arg,'C',options.C,'model',model);
set(handles.pushbutton2,'userdata',D);
text=sprintf(...
['訓(xùn)練算法:OAA算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n'],...
ker,T);
case 2
data=get(handles.axes1,'userdata');
options.solver = 'smo';
options.ker = ker;
options.arg = 1;
options.C = C;
T1=cputime;
model = oaosvm(data,options);
T=cputime-T1;
set(handles.pushbutton6,'userdata',T);
axes(handles.axes1);cla;
ppatterns(data);
D=struct('solver',options.solver,'ker',options.ker,'arg',options.arg,'C',options.C,'model',model);
set(handles.pushbutton2,'userdata',D);
text=sprintf(...
['訓(xùn)練算法:OAO算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n'],...
ker,T);
case 3
data=get(handles.axes1,'userdata');
options.solver = 'smo'; % 使用 smo solver
options.ker = ker;
options.arg = 1;
options.C = 10;
T1=cputime;
model = bsvm2(data,options); % training
T=cputime-T1;
set(handles.pushbutton6,'userdata',T);
% 顯示圖形
axes(handles.axes1);cla;
ppatterns(data);
D=struct('solver',options.solver,'ker',options.ker,'arg',options.arg,'C', options.C,'model',model);
set(handles.pushbutton2,'userdata',D);
text=sprintf(...
['訓(xùn)練算法:BSVM2算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n'],...
ker,T);
end
set( handles.edit1,'String', text );%將結(jié)果顯示到注釋窗口
%關(guān)閉窗口回調(diào)函數(shù)
function pushbutton5_Callback(hObject, eventdata, handles)
close(gcf);
%圖象導(dǎo)出回調(diào)函數(shù)
function pushbutton6_Callback(hObject, eventdata, handles)
get(gcf,'CurrentAxes');
fig2jpg(gcf);
%計(jì)算SV回調(diào)函數(shù)
function pushbutton7_Callback(hObject, eventdata, handles)
data=get(handles.axes1,'userdata');
D=get(handles.pushbutton2,'userdata');
model=D.model;
axes(handles.axes1);cla;
nsv = model.nsv;%將訓(xùn)練算法中支持向量個(gè)數(shù)的變量nsv傳遞出來(lái)
time=get(handles.pushbutton6,'userdata');%將計(jì)算CPU時(shí)間參數(shù)T傳遞出來(lái)
ppatterns(data);
ppatterns(model.sv.X,'ko',12);
set(handles.pushbutton2,'userdata',D);
ker_inx = get( handles.popupmenu2, 'Value' ); %獲取核函數(shù)信息
if ker_inx == 1;
ker = 'linear';
elseif ker_inx == 2;
ker = 'rbf';
elseif ker_inx == 3;
ker = 'poly';
else
ker = 'sigmoid';
end
%要輸出的內(nèi)容控制
if get( handles.popupmenu1, 'Value' )==1
text1=sprintf(...
['訓(xùn)練算法:OAA算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n',...
'支持向量的個(gè)數(shù): %d\n'],...
ker,time,nsv );
elseif get( handles.popupmenu1, 'Value' )==2
text1=sprintf(...
['訓(xùn)練算法:OAO算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n',...
'支持向量的個(gè)數(shù): %d\n'],...
ker,time,nsv );
else
text1=sprintf(...
['訓(xùn)練算法:BSVM2算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n',...
'支持向量的個(gè)數(shù): %d\n'],...
ker,time,nsv );
end
set( handles.edit1,'String', text1 );
%決策邊界回調(diào)函數(shù)
function pushbutton8_Callback(hObject, eventdata, handles)
data=get(handles.axes1,'userdata');
D=get(handles.pushbutton2,'userdata');
model=D.model;
axes(handles.axes1);cla;
ppatterns(data);
ppatterns(model.sv.X,'ko',12);
pboundary(model);
nsv = model.nsv;
time=get(handles.pushbutton6,'userdata');
trnerr = model.trnerr;%將訓(xùn)練算法中計(jì)算分類(lèi)錯(cuò)誤的參數(shù)傳出
ker_inx = get( handles.popupmenu2, 'Value' ); %獲取核函數(shù)信息
if ker_inx == 1;
ker = 'linear';
elseif ker_inx == 2;
ker = 'rbf';
elseif ker_inx == 3;
ker = 'poly';
else
ker = 'sigmoid';
end
if get( handles.popupmenu1, 'Value' )==1
text1=sprintf(...
['訓(xùn)練算法:OAA算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n',...
'支持向量的個(gè)數(shù): %d\n',...
'分類(lèi)錯(cuò)誤率: %.2f%%'],...
ker,time,nsv ,100*trnerr);
elseif get( handles.popupmenu1, 'Value' )==2
text1=sprintf(...
['訓(xùn)練算法:OAO算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n',...
'支持向量的個(gè)數(shù): %d\n',...
'分類(lèi)錯(cuò)誤率: %.2f%%'],...
ker,time,nsv ,100*trnerr );
else
text1=sprintf(...
['訓(xùn)練算法:BSVM2算法\n',...
'核函數(shù): %s \n',...
'訓(xùn)練所費(fèi)時(shí)間: %.6f\n',...
'支持向量的個(gè)數(shù): %d\n',...
'分類(lèi)錯(cuò)誤率: %.2f%%'],...
ker,time,nsv,100*trnerr );
end
set( handles.edit1,'String', text1 );
%訓(xùn)練算法下拉菜單
function popupmenu1_Callback(hObject, eventdata, handles)
function popupmenu1_CreateFcn(hObject, eventdata, handles)
handles.popupmenu1=get(hObject,'Value');%將菜單選項(xiàng)值傳出
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% 核函數(shù)下拉菜單
function popupmenu2_Callback(hObject, eventdata, handles)
function popupmenu2_CreateFcn(hObject, eventdata, handles)
handles.popupmenu2=get(hObject,'Value');%將菜單選項(xiàng)值傳出
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% C懲罰系數(shù)下拉菜單
function popupmenu3_Callback(hObject, eventdata, handles)
function popupmenu3_CreateFcn(hObject, eventdata, handles)
handles.popupmenu3=get(hObject,'Value');%將菜單選項(xiàng)值傳出
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
%注釋窗口
function edit1_Callback(hObject, eventdata, handles)
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% MENU設(shè)計(jì)及回調(diào)函數(shù)
%創(chuàng)建數(shù)據(jù)
function Untitled_2_Callback(hObject, eventdata, handles)
n=5;
createdata('finite',n,'created',gcf);
%裝載數(shù)據(jù)
function Untitled_3_Callback(hObject, eventdata, handles)
h = waitbar(0,'Please wait...');
for i=1:100,
waitbar(i/100)
end
close(h)
data=get( gcf,'UserData');
[name,path]=uigetfile('*.mat','Open file');
if name~=0,
file.pathname=strcat(path,name);
file.path=path;
file.name=name;
if check2ddata( file.pathname )
set(data.pushbutton2,'UserData',file);
cla;
data=loadsets(gcf);
set(gcf,'UserData',data);
set(data.edit1,'String','數(shù)據(jù)已經(jīng)裝載,請(qǐng)選擇下一步 !');
else
errordlg('This file does not contain required data.','Bad file','modal');
end
end
% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% 關(guān)閉
function Untitled_4_Callback(hObject, eventdata, handles)
close(gcf);
% --------------------------------------------------------------------
function Untitled_5_Callback(hObject, eventdata, handles)
% 調(diào)用OAA算法演示模型
function Untitled_6_Callback(hObject, eventdata, handles)
OAA_Multi_C_SVM;
% 調(diào)用OAO算法演示模型
function Untitled_7_Callback(hObject, eventdata, handles)
OAO_Multi_SVM;
% 調(diào)用BSVM2算法演示模型
function Untitled_8_Callback(hObject, eventdata, handles)
Multi_BSVM__L2_soft_margi
% --------------------------------------------------------------------
function Untitled_9_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function Untitled_10_Callback(hObject, eventdata, handles)
helpwin(mfilename);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -