资源简介

使用matlab实现id3决策树算法,给出使用方法及举例,并附带相关测试集合。

资源截图

代码片段和文件信息

%author: svmsunny
%mailto: chingsun00@gmail.com

classdef ID3Tree
    
    properties 
        translator;  %for translating the data type from string to number if necessary;
        statistic_info; %for each dimension gather statistics
        root;
        train_set_dim;
    end
    
    methods
        %function ========================================================
        function obj = construct_tree(obj training_set data_type) 
            %training_set 
            %data_type: 1 for string and 0 for number. donnot consider
            %the condition that some column are number and others are
            %string.
            %生成一个分类树
            
            if data_type   %translate the string data to number
                translator = trans_data();
                [trans_train obj.translator] = translator.translate_train(training_set); 
                clear training_set;
                training_set = trans_train;
            end
            
            obj.train_set_dim = size(training_set2);
            
            for i = 1 : size(training_set2)  %column
                clear table;
                table = tabulate(training_set(:i)); 
                cout = 1;
                for j = 1 : size(table1) %
                    if table(j2)   %not zero
                        statistic_info{1i}(1cout) = table(j1);
                        cout = cout + 1;
                    end
                end
            end
            obj.statistic_info = statistic_info;
            dim_visible = ones(1 size(training_set2)-1);
            
            obj.root = make_tree(training_setdim_visiblestatistic_info);
        end
        
        %function ========================================================
        function [cor_rationtargets] = classify(obj testing_set)
            %
            if ~isempty(obj.translator)  % need to translate the data
                trans_test = obj.translator.translate_test(testing_set);
                clear testing_set;
                testing_set = trans_test;
            end      
            train_set_dimension = obj.train_set_dim;  %include the class label column
            classifier_tree = obj.root;
            
            targets = zeros(size(testing_set1) 1);
            for i = 1: size(testing_set 1)
                targets(i 1) = classify_class(classifier_tree testing_set(i1:train_set_dimension-1));
            end
            
            if train_set_dimension == size(testing_set 2)  %same dimension then get the correct ratio
                cout = 0;
                for i = 1: size(testing_set 1)
                    if testing_set(i end) == targets(i 1)
                        cout = cout + 1;
                    end
                end
                cor_ration = cout / size(testing_set 1);
            else
                cor_ration = -1; %not for testing the classifying Precision
       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       7972  2010-04-28 21:23  id3决策树\ID3Tree.m

     文件      16416  2010-04-21 11:24  id3决策树\orig_abalone_test.txt

     文件      62947  2010-04-21 11:25  id3决策树\orig_abalone_train.txt

     文件       3317  2010-04-28 10:28  id3决策树\trans_data.m

     文件        530  2010-04-28 21:19  id3决策树\usecase.m

     目录          0  2010-04-28 21:20  id3决策树

----------- ---------  ---------- -----  ----

                91182                    6


评论

共有 条评论