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

📄 classifier_commands.m

📁 数据挖掘matlab源码
💻 M
📖 第 1 页 / 共 2 页
字号:
function classifier_commands(command)

%This function processes events from the main (single-algorithm) GUI screen

persistent Preprocessing_methods;
persistent Algorithms;
if (isempty(Preprocessing_methods) | isempty(Algorithms)),
    Algorithms = read_algorithms('Classification.txt');
    Preprocessing_methods = read_algorithms('Preprocessing.txt');
end

switch(command)
    
case 'Init'
    %Init of the classifier GUI
    h				= findobj('Tag', 'Preprocessing');
    set(h,'String',strvcat(Preprocessing_methods(:).Name));
    chosen = strmatch('None',char(Preprocessing_methods(:).Name));
    set(h,'Value',chosen);
    
    h				= findobj('Tag', 'Algorithm');
    set(h,'String',strvcat(Algorithms(:).Name));
    chosen = strmatch('LS',char(Algorithms(:).Name));
    set(h,'Value',chosen);
    
case 'Changed Preprocessing'
    h				= findobj(gcbf, 'Tag', 'Preprocessing');
    chosen       	= get(h, 'Value');
    
    hLabel	  		= findobj(gcbf, 'Tag', 'lblPreprocessingParameters');
    hBox		  		= findobj(gcbf, 'Tag', 'txtPreprocessingParameters');
    set(hBox,'Visible','on');
    
    if ~isempty(chosen),
        set(hLabel,'String',Preprocessing_methods(chosen).Caption);
        set(hBox,'String',Preprocessing_methods(chosen).Default);
        if strcmp(char(Preprocessing_methods(chosen).Field),'N')
            set(hLabel,'String','');
            set(hBox,'Visible','off');    
        end
    else
        set(hLabel,'String','');
        set(hBox,'Visible','off');    
    end
    
case 'Changed Algorithm'
    h					= findobj(gcbf, 'Tag', 'Algorithm');
    chosen             	= get(h, 'Value');
    
    hLabel	  		= findobj(gcbf, 'Tag', 'lblAlgorithmParameters');
    hBox	 		= findobj(gcbf, 'Tag', 'txtAlgorithmParameters');
    hLongBox		= findobj(gcbf, 'Tag', 'txtAlgorithmParametersLong');
    set(hBox,'Visible','on');
    
    if ~isempty(chosen),
        set(hLabel,'String',Algorithms(chosen).Caption);
        switch Algorithms(chosen).Field
        case 'S'
            set(hBox,'String',Algorithms(chosen).Default);
            set(hBox,'Visible','on');
            set(hLongBox,'Visible','off');
        case 'L'
            set(hLongBox,'String',Algorithms(chosen).Default);
            set(hBox,'Visible','off');
            set(hLongBox,'Visible','on');
        case 'N'
            set(hLabel,'String','');
            set(hBox,'Visible','off');    
            set(hLongBox,'Visible','off');
        end
    else
        set(hLabel,'String','');
        set(hBox,'Visible','off');    
        set(hLongBox,'Visible','off');
    end
    
case 'Start'
    
    %Start the classification process
    Npoints = 100; %Number of points on each axis of the decision region

    %Read data from the workspace
    hm = findobj('Tag', 'Messages'); 
    hParent = get(hm,'Parent'); %Get calling window tag
    set(hm,'String',''); 
    
    h =  findobj('Tag', 'TestSetError');
    set(h, 'String', '');
    h =  findobj('Tag', 'TrainSetError');
    set(h, 'String', '');
    
    %Do some error checking
    if evalin('base', '~exist(''targets'')')    
        set(hm,'String','No targets on workspace. Please load targets.')   
        break
    end
    
    if evalin('base', '~exist(''features'')')    
        set(hm,'String','No features on workspace. Please load features.')
        break
    end 

    features                = evalin('base','features');
    targets                 = evalin('base','targets');    
    if (evalin('base', 'exist(''distribution_parameters'')')),
	    distribution_parameters = evalin('base', 'distribution_parameters');
    end
    
    error_method_val = get(findobj('Tag', 'popErrorEstimationMethod'),'Value');
    error_method_str = get(findobj('Tag', 'popErrorEstimationMethod'),'String');
    error_method 	 = char(error_method_str(error_method_val(1)));
        
    h = findobj('Tag', 'Redraws');   
    redraws = str2num(get(h, 'String'));   
    if isempty(redraws), 
        set(hm,'String','Please select how many redraws are needed.')      
        break
    else     
        if (redraws < 1), 
            set(hm,'String','Number of redraws must be larger than 0.')     
            break    
        else
            if (strcmp(error_method, 'Cross-Validation') & (redraws == 1)),
                set(hm,'String','Number of redraws must be larger than 1.')     
                break    
            end        
        end   
    end      
    
    h = findobj('Tag', 'PercentTraining'); 
    percent = str2num(get(h, 'String'));   
    if strcmp(error_method, 'Holdout'),
        if isempty(percent), 
            set(hm,'String','Please select the percentage of training vectors.')     
            break
        else     
            if (floor(percent/100*length(targets)) < 1),     
                set(hm,'String','Number of training vectors must be larger than 0.')     
                break    
            end   
            if (percent >= 100),     
                set(hm,'String','Number of training vectors must be smaller than 100%.')     
                break    
            end   
            if (floor((1-percent/100)*length(targets)) < 1),     
                set(hm,'String','Number of test vectors must be larger than 0.')     
                break    
            end   
        end
    end      
    
    %Find the region for the grid
    [region,x,y]  = calculate_region(features, [zeros(1,4) Npoints]);    
    
    h		      = findobj('Tag', 'Algorithm'); 
    val		      = get(h,'Value');     
    algorithm     = get(h,'String'); 
    algorithm 	  = deblank(char(algorithm(val,:)));
    h		      = findobj('Tag', 'Preprocessing'); 
    val		      = get(h,'Value');     
    preprocessing = get(h,'String'); 
    preprocessing = deblank(char(preprocessing(val,:)));
    
    %Get algorithm paramters. If they don't contain a string, turn them into a number
    if strcmp(get(findobj('Tag', 'txtAlgorithmParameters'),'Visible'),'on'),
        AlgorithmParameters = char(get(findobj('Tag', 'txtAlgorithmParameters'),'String'));
    else
        AlgorithmParameters = char(get(findobj('Tag', 'txtAlgorithmParametersLong'),'String'));
    end
    if (~isempty(str2num(AlgorithmParameters))),
        AlgorithmParameters = str2num(AlgorithmParameters);
    end
    
    if strcmp(get(findobj('Tag', 'txtPreprocessingParameters'),'Visible'),'on'),
        PreprocessingParameters = char(get(findobj('Tag', 'txtPreprocessingParameters'),'String'));
    else
        PreprocessingParameters = char(get(findobj('Tag', 'txtPreprocessingParameters'),'String'));
    end
    if (~isempty(str2num(PreprocessingParameters))),
        PreprocessingParameters = str2num(PreprocessingParameters);
    end
    
    plot_on = strcmp(get(findobj(gcbf,'Label','&Show center of partitions during training'),'Checked'),'on');
    
    SepratePreprocessing = strcmp(get(findobj(hParent, 'Tag', '&Options&SeparatePreprocessing'),'Checked'),'on');
    
    %Now that the data is OK, start working
    set(gcf,'pointer','watch');
    hold off
    plot_scatter(features, targets, hParent)
    axis(region(1:4))
    hold on
    drawnow

    %Call the main classification function
    [D, test_err, train_err] = start_classify(features, targets, error_method, redraws, percent, preprocessing, PreprocessingParameters, ...
                                              algorithm, AlgorithmParameters, region, hm, SepratePreprocessing, plot_on);

    %Put results on the workspace as well
    assignin('base', 'D', D);
    assignin('base', 'test_err', test_err);
    assignin('base', 'train_err', train_err);
    assignin('base', 'region', region);

    %Display error
    Nclasses = length(unique(targets));
    h =  findobj('Tag', 'TestSetError');
    s = 'Test set errors: ';
    for j = 1:Nclasses,
        s = [s 'Class ' num2str(j) ': ' num2str(mean(test_err(j,:)),2) '. '];
    end
    s = [s 'Total: ' num2str(mean(test_err(Nclasses+1,:)),2)];
    set(h, 'String', s);
    
    h =  findobj('Tag', 'TrainSetError');
    
    h =  findobj('Tag', 'TrainSetError');
    s = 'Train set errors: ';
    for j = 1:Nclasses,
        s = [s 'Class ' num2str(j) ': ' num2str(mean(train_err(j,:)),2) '. '];
    end
    s = [s 'Total: ' num2str(mean(train_err(Nclasses+1,:)),2)];
    set(h, 'String', s);
    
    %Show decision region
    [s,h]=contour(x,y,D,1);set(h,'LineWidth',2,'EdgeColor','k');
    if strcmp(get(findobj(gcbf,'Label','Shade &Decision Regions'),'Checked'),'on'),
        contourf(x,y,D,1);colormap([198 198 255; 240 255 240]/255);
    end
    
    %Show Bayes decision region and error (if possible)
    hBayes = findobj('Tag','chkBayes');
    if ((get(hBayes, 'Value')) & (exist('distribution_parameters'))),
        Dbayes = decision_region(distribution_parameters, region);
        [s,h]	 = contour(x,y,Dbayes,1);set(h,'LineWidth',1,'EdgeColor','r');
        [classify, Bayes_err]  = classification_error(Dbayes, features, targets, region);
        h =  findobj('Tag', 'BayesError');
        s = 'Bayes errors: ';
        for j = 1:Nclasses,
            s = [s 'Class ' num2str(j) ': ' num2str(1 - classify(j,j),2) '. '];
        end
        s = [s 'Total: ' num2str(mean(1 - diag(classify)),2)];
        set(h, 'String', s);
        assignin('base', 'Dbayes', Dbayes);    
    end   
    
    %Draw grid if neccessary
    h = findobj('Label', '&Grid');
    if strcmp('on',get(h, 'Checked')),
        grid on
        set(gca,'layer','top')
    else
        grid off
    end
    
    %Replot training points if necessary
    if strcmp(get(findobj(gcbf,'Label','Show &Training points'),'Checked'),'on'),
        plot_scatter(train_features, train_targets, hParent, 2)
    end
    
    hold off
    
    %That's all folks!
    s = 'Finished!';
    if (redraws > 1),
        s = [s ' (Note that only the last decision region is shown)'];
    end
    
    set(hm, 'String', s);   
    set(gcf,'pointer','arrow');
    
case 'Compare'
    %Start the algorithm comparison screen
    h   = multialgorithms;
    h1  = findobj(h, 'Tag', 'lstAllAlgorithms');
    set(h1, 'String', strvcat(Algorithms(:).Name));
    
case 'FileNameInput'
    evalin('base','hold off')
    evalin('base','clear distribution_parameters features targets')
    evalin('base','h =  findobj(''Tag'', ''BayesError'');')
    evalin('base','set(h, ''String'', '''');')
    evalin('base','h =  findobj(''Tag'', ''TestSetError'');')
    evalin('base','set(h, ''String'', '''');')
    evalin('base','h =  findobj(''Tag'', ''TrainSetError'');')
    evalin('base','set(h, ''String'', '''');')
    evalin('base','region = [0 0 0 0 500];')
    evalin('base','[features, targets, distribution_parameters] = load_file(get(gcbo, ''String''), region);')
    if (evalin('base', 'isempty(distribution_parameters)')),
        evalin('base', 'clear distribution_parameters');
    end
    
    if (evalin('base', '~isempty(features)'))
        evalin('base','region = calculate_region(features, [zeros(1,4) 100]);')
    end
    
case 'SearchForFile'
    evalin('base','region = [0 0 0 0 500];')
    evalin('base','[filename, pathname] = uigetfile(''*.mat'', ''Open file ...'');')
    if (evalin('base','filename ~= 0')),
        evalin('base','clear distribution_parameters features targets')
        evalin('base','h =  findobj(''Tag'', ''BayesError'');')
        evalin('base','set(h, ''String'', '''');')
        evalin('base','h =  findobj(''Tag'', ''TestSetError'');')
        evalin('base','set(h, ''String'', '''');')

⌨️ 快捷键说明

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