0

マルチクラス用に Naive Bayse Classifier を実装しましたが、トレーニング データ セットを増やしてもエラー率が同じであるという問題があります。私はこれを何度もデバッグしていましたが、なぜそれが起こっているのか理解できませんでした。だから、ここに投稿して、何か間違ったことをしていないかどうかを調べようと思いました。

%Naive Bayse Classifier
%This function split data to 80:20 as data and test, then from 80
%We use incremental 5,10,15,20,30 as the test data to understand the error
%rate. 
%Goal is to compare the plots in stanford paper
%http://ai.stanford.edu/~ang/papers/nips01-discriminativegenerative.pdf

function[tPercent] = naivebayes(file, iter, percent)
dm = load(file);
    for i=1:iter

        %Getting the index common to test and train data
        idx = randperm(size(dm.data,1))

        %Using same idx for data and labels
        shuffledMatrix_data = dm.data(idx,:);
        shuffledMatrix_label = dm.labels(idx,:);

        percent_data_80 = round((0.8) * length(shuffledMatrix_data));


        %Doing 80-20 split
        train = shuffledMatrix_data(1:percent_data_80,:);

        test = shuffledMatrix_data(percent_data_80+1:length(shuffledMatrix_data),:);

        %Getting the label data from the 80:20 split
        train_labels = shuffledMatrix_label(1:percent_data_80,:);

        test_labels = shuffledMatrix_label(percent_data_80+1:length(shuffledMatrix_data),:);

        %Getting the array of percents [5 10 15..]
        percent_tracker = zeros(length(percent), 2);

        for pRows = 1:length(percent)

            percentOfRows = round((percent(pRows)/100) * length(train));
            new_train = train(1:percentOfRows,:);
            new_train_label = train_labels(1:percentOfRows);

            %get unique labels in training
            numClasses = size(unique(new_train_label),1);
            classMean = zeros(numClasses,size(new_train,2));
            classStd = zeros(numClasses, size(new_train,2));
            priorClass = zeros(numClasses, size(2,1));

            % Doing the K class mean and std with prior
            for kclass=1:numClasses
                classMean(kclass,:) = mean(new_train(new_train_label == kclass,:));
                classStd(kclass, :) = std(new_train(new_train_label == kclass,:));
                priorClass(kclass, :) = length(new_train(new_train_label == kclass))/length(new_train);
            end

            error = 0;

            p = zeros(numClasses,1);

            % Calculating the posterior for each test row for each k class
            for testRow=1:length(test)
                c=0; k=0;
                for class=1:numClasses
                    temp_p = normpdf(test(testRow,:),classMean(class,:), classStd(class,:));
                    p(class, 1) = sum(log(temp_p)) + (log(priorClass(class)));
                end
                %Take the max of posterior 
                [c,k] = max(p(1,:));
                if test_labels(testRow) ~= k
                    error = error +  1;
                end
            end
            avgError = error/length(test);
            percent_tracker(pRows,:) = [avgError percent(pRows)];
            tPercent = percent_tracker;
            plot(percent_tracker)
        end
    end
end

ここに私のデータの次元があります

x = 

      data: [768x8 double]
    labels: [768x1 double]

UCI の Pima データセットを使用しています

4

1 に答える 1

2

トレーニング データ自体の実装の結果は何ですか? それはまったく合っていますか?

確認するのは難しいですが、私が気づいたことがいくつかあります。

  1. すべてのクラスがトレーニング データを持つことが重要です。トレーニング データがなければ、クラスを認識するように分類器を実際にトレーニングすることはできません。
  2. 可能であれば、トレーニング例の数を一部のクラスに偏らせてはなりません。たとえば、2 クラス分類で、クラス 1 のトレーニングとクロス検証の例の数がデータの 5% しか構成されていない場合、常にクラス 2 を返す関数には 5% の誤差があります。精度と再現率を別々にチェックしてみましたか?
  3. クラス内の各機能に正規分布を適合させ、それを事後確率に使用しようとしています。スムージングの観点からそれがどのように機能するかはわかりません。単純なカウントで再実装して、異なる結果が得られるかどうかを確認していただけますか?
  4. また、機能が非常に冗長であり、ベイズ法が確率を過大評価している可能性もあります。
于 2012-10-07T09:45:47.137 に答える