📄 demospectralclustering.m
字号:
function varargout = DemoSpectralClustering(varargin)
% Usage: DemoSpectralClustering()
%
% Opens a large window, with all sorts of knobs and sliders. Just play :-)
%
% Documentation can be found at the GraphDemo webpage.
% Written by Matthias Hein and Ulrike von Luxburg
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DemoSpectralClustering_OpeningFcn, ...
'gui_OutputFcn', @DemoSpectralClustering_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
% End initialization code - DO NOT EDIT
% --- Executes just before DemoSpectralClustering is made visible.
function DemoSpectralClustering_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to DemoSpectralClustering (see VARARGIN)
% Choose default command line output for DemoSpectralClustering
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% ULE:
% to avoid the following weird error message:
% Warning: RGB color data not yet supported in Painter's mode.
% is due to automatic choice of renderer by matlab.
set(gcf, 'Renderer', 'Zbuffer')
warning('off','MATLAB:dispatcher:InexactMatch')
global ALLDATA;
global SCDATA;
ALLDATA = struct('Dim' , 3, 'MinDim' , 2, 'MaxDim' , 200, 'Num', 500, 'NumKNN', 10, 'MinKNN', 1, 'MaxKNN', 200, 'Eps', 0.3, 'MinEps', 0.01, 'MaxEps', 10.0, 'Density', 1, 'x',0, 'y', 0, 'Labeled', 0, 'GraphType', 1, 'K', 0, 'Gamma',1,'MaxGamma',10,'MinGamma',0.1);
SCDATA = struct( 'Output',0, 'NumCluster', 2, 'MinCluster', 2, 'MaxCluster', 9);
set(gcf, 'Name', 'Demo Spectral Clustering', ...
'Units', 'normalized', ... %units of measurement used to interpret the position vector
'Visible','off');
% set controls to the initial values
set(handles.SldKNN,'Value',(ALLDATA.NumKNN-ALLDATA.MinKNN)/(ALLDATA.MaxKNN-ALLDATA.MinKNN));
set(handles.SldNrClusters,'Value',(SCDATA.NumCluster-SCDATA.MinCluster)/(SCDATA.MaxCluster-SCDATA.MinCluster));
set(handles.SldWeights,'Value',(ALLDATA.Gamma-ALLDATA.MinGamma)/(ALLDATA.MaxGamma-ALLDATA.MinGamma));
% set all static text elements
set(handles.TxtKNN,'String',['K=',num2str(ALLDATA.NumKNN)]);
set(handles.TxtWeights,'String',['Sigma =',num2str(ALLDATA.Gamma)]);
set(handles.TxtNrClusters,'String',['N=',num2str(SCDATA.NumCluster)]);
set(handles.TxtDim,'String',num2str(ALLDATA.Dim));
% update slider for labeled data
set(handles.SldNrClusters,'SliderStep',[1/(SCDATA.MaxCluster-SCDATA.MinCluster),1/(SCDATA.MaxCluster-SCDATA.MinCluster)]);
% generate data (inital dataset: two moons with balanced classes)
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(2,ALLDATA.Num,3,[0.5,0.5],0.01);
UpdateALL(handles)
% --- Outputs from this function are returned to the command line.
function varargout = DemoSpectralClustering_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on slider movement.
function SldKNN_Callback(hObject, eventdata, handles)
% hObject handle to SldKNN (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global ALLDATA;
value = get(hObject,'Value');
if(ALLDATA.GraphType<2)
ALLDATA.NumKNN = floor(value*(ALLDATA.MaxKNN-ALLDATA.MinKNN) + ALLDATA.MinKNN);
set(handles.TxtKNN,'String',['K=',num2str(ALLDATA.NumKNN)]);
else
ALLDATA.Eps = value*(ALLDATA.MaxEps-ALLDATA.MinEps) + ALLDATA.MinEps;
set(handles.TxtKNN,'String',['Eps=',num2str(ALLDATA.Eps,'%2.2f')]);
end
%UpdateALL(handles);
% --- Executes during object creation, after setting all properties.
function SldKNN_CreateFcn(hObject, eventdata, handles)
% hObject handle to SldKNN (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function SldWeights_Callback(hObject, eventdata, handles)
% hObject handle to SldWeights (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global ALLDATA;
value = get(hObject,'Value');
ALLDATA.Gamma = value*(ALLDATA.MaxGamma-ALLDATA.MinGamma) + ALLDATA.MinGamma;
set(handles.TxtWeights,'String',['Sigma =',num2str(ALLDATA.Gamma,'%2.1f')]);
%UpdateALL(handles);
% --- Executes during object creation, after setting all properties.
function SldWeights_CreateFcn(hObject, eventdata, handles)
% hObject handle to SldWeights (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function SldNrClusters_Callback(hObject, eventdata, handles)
% hObject handle to SldNrClusters (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global SCDATA;
value = get(hObject,'Value');
SCDATA.NumCluster = floor(value*(SCDATA.MaxCluster-SCDATA.MinCluster) + SCDATA.MinCluster);
set(handles.TxtNrClusters,'String',['N=',num2str(SCDATA.NumCluster)]);
%UpdateALL(handles);
% --- Executes during object creation, after setting all properties.
function SldNrClusters_CreateFcn(hObject, eventdata, handles)
% hObject handle to SldNrClusters (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on selection change in MenuData.
function MenuData_Callback(hObject, eventdata, handles)
% hObject handle to MenuData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns MenuData contents as cell array
% contents{get(hObject,'Value')} returns selected item from MenuData
global ALLDATA;
axes(handles.AxsData);
cla;
popup_sel_index = get(handles.MenuData, 'Value');
switch popup_sel_index
case 1 % Two Moons with balanced classes [0.5,0.5]
ALLDATA.Density=2;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.5,0.5],0.01);
case 2 % Two Moons with unbalanced classes [0.2,0.8]
ALLDATA.Density=2;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.2,0.8],0.01);
case 3 % Two isotropic Gaussians balanced classes [0.5,0.5]
ALLDATA.Density=3;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.5,0.5,0],0.01);
case 4 % Two isotropic Gaussians unbalanced classes [0.2,0.8]
ALLDATA.Density=3;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.2,0.8,0],0.01);
case 5 % Two isotropic Gaussians with different variance and balanced classes [0.2,0.8]
ALLDATA.Density=4;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.5,0.5,0],0.01);
case 6 % Three isotropic Gaussians almost balanced [0.3,0.3,0.4]
ALLDATA.Density=3;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.3,0.3,0.4],0.01);
end
UpdateALL(handles);
% --- Executes during object creation, after setting all properties.
function MenuData_CreateFcn(hObject, eventdata, handles)
% hObject handle to MenuData (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in MenuGraph.
function MenuGraph_Callback(hObject, eventdata, handles)
% hObject handle to MenuGraph (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = get(hObject,'String') returns MenuGraph contents as cell array
% contents{get(hObject,'Value')} returns selected item from MenuGraph
global ALLDATA;
value = get(hObject,'Value');
switch value
case 1, ALLDATA.GraphType = 1; set(handles.TxtNrNN,'String','Number K of Neighbors');
value = (ALLDATA.NumKNN - ALLDATA.MinKNN)/(ALLDATA.MaxKNN - ALLDATA.MinKNN);
set(handles.SldKNN,'Value',value);
set(handles.TxtKNN,'String',['K=',num2str(ALLDATA.NumKNN,'%d')]);
case 2, ALLDATA.GraphType = 0; set(handles.TxtNrNN,'String','Number K of Neighbors');
value = (ALLDATA.NumKNN - ALLDATA.MinKNN)/(ALLDATA.MaxKNN - ALLDATA.MinKNN);
set(handles.SldKNN,'Value',value);
set(handles.TxtKNN,'String',['K=',num2str(ALLDATA.NumKNN,'%d')]);
case 3, if(ALLDATA.Density~=6) ALLDATA.GraphType = 2;
set(handles.TxtNrNN,'String','Epsilon Parameter');
value = (ALLDATA.Eps - ALLDATA.MinEps)/(ALLDATA.MaxEps - ALLDATA.MinEps);
set(handles.SldKNN,'Value',value);
set(handles.TxtKNN,'String',['Eps=',num2str(ALLDATA.Eps,'%2.2f')]);
else
if(ALLDATA.GraphType==1)
set(handles.PopGraphType,'Value',1);
end
if(ALLDATA.GraphType==0)
set(handles.PopGraphType,'Value',2);
end
end
end
%UpdateALL(handles);
% --- Executes during object creation, after setting all properties.
function MenuGraph_CreateFcn(hObject, eventdata, handles)
% hObject handle to MenuGraph (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on slider movement.
function SldDim_Callback(hObject, eventdata, handles)
% hObject handle to SldDim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global ALLDATA;
value = get(hObject,'Value');
ALLDATA.Dim = floor(value*(ALLDATA.MaxDim-ALLDATA.MinDim) + ALLDATA.MinDim);
set(handles.TxtDim,'String',num2str(ALLDATA.Dim));
popup_sel_index = get(handles.MenuData, 'Value');
switch popup_sel_index
case 1 % Two Moons with balanced classes [0.5,0.5]
ALLDATA.Density=2;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.5,0.5],0.01);
case 2 % Two Moons with unbalanced classes [0.2,0.8]
ALLDATA.Density=2;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.2,0.8],0.01);
case 3 % Two isotropic Gaussians balanced classes [0.5,0.5]
ALLDATA.Density=3;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.5,0.5,0],0.01);
case 4 % Two isotropic Gaussians unbalanced classes [0.2,0.8]
ALLDATA.Density=3;
% generate data
ALLDATA.Num=500;
[ALLDATA.x,ALLDATA.y]=GD_GenerateData(ALLDATA.Density,ALLDATA.Num,ALLDATA.Dim,[0.2,0.8,0],0.01);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -