UCI 機械学習チームによって公開されたデータセットを使用して、単純ベイズ分類子を実装しようとしています。私は機械学習に不慣れで、仕事関連の問題に使用する手法を理解しようとしているので、最初に理論を理解する方がよいと考えました。
私は pima データセット ( Link to Data - UCI-ML ) を使用しています。私の目標は、K クラスの問題に対して Naive Bayes Univariate Gaussian Classifier を構築することです (データは K=2 の場合のみ存在します)。私はデータの分割を行い、各クラスの平均、標準偏差、各クラスの事前確率を計算しましたが、この後、何をどのように行うべきかわからないため、行き詰まりました。事後確率を計算するべきだと感じていますが、
これが私のコードです。ベクトルとしてパーセントを使用しています。トレーニング データのサイズを 80:20 分割から増やしたときの動作を確認したいからです。基本的に [10 20 30 40] を渡すと、80:20 分割からそのパーセンテージが取得され、80% の 10% がトレーニングとして使用されます。
function[classMean] = naivebayes(file, iter, percent)
dm = load(file);
for i=1:iter
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),:);
train_labels = shuffledMatrix_label(1:percent_data_80,:)
test_labels = shuffledMatrix_data(percent_data_80+1:length(shuffledMatrix_data),:);
%Getting the array of percents
for pRows = 1:length(percent)
percentOfRows = round((percent(pRows)/100) * length(train));
new_train = train(1:percentOfRows,:)
new_trin_label = shuffledMatrix_label(1:percentOfRows)
%get unique labels in training
numClasses = size(unique(new_trin_label),1)
classMean = zeros(numClasses,size(new_train,2));
for kclass=1:numClasses
classMean(kclass,:) = mean(new_train(new_trin_label == kclass,:))
std(new_train(new_trin_label == kclass,:))
priorClassforK = length(new_train(new_trin_label == kclass))/length(new_train)
priorClassforK_1 = 1 - priorClassforK
end
end
end
end